yeah

搜索

计数器

64556

链接

运用JS将搜索的关键字高亮显示
createtextrange createrange有什么区别

js为选中的文本加链接

yeah posted @ Jan 06, 2010 07:02:00 PM in PHP学习 , 1660 阅读

selection是文档中被选择的所有对象   
createRange()是将创建一个textRange()对象,就是文档中被选择的所有文本都放到这个对象里了   
pasteHTML()将被选中的文本清空,然后将给定的HTML串粘贴进来

例子:
<script type="text/javascript">
        function addLink(){
           var oRange = document.selection.createRange();
           if(oRange.text!=''){
              var oUrl = window.prompt('链接网址...','http://www.163.com/');
              var oHtml = '<a href=' oUrl ' target=_blank>' oRange.text '</a>';
              alert(oHtml);
              oRange.pasteHTML(oHtml);
           }else{
              window.alert('您没有选择加链接的文字!');
           }
        }
</Script>

<input type="button" value="添加链接" onclick="addLink();" >
<area>hehe</area>


Tags -
celebrity heights 说:
Apr 21, 2022 02:19:40 PM

Billie Eilish - the youngest person and second person ever to win the four main Grammy categories, was born in December 18, 2001, find out more about her info on celeb height wiki


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter