js给选择文字添加链接

  js如何给选中的文字添加链接,就是选中的文字用a对象包裹起来。

<script>
    function surroundContent() {
        var r;
        if (document.selection) {
            r = document.selection.createRange();
            if (r.text != '') r.pasteHTML('<a href="#">' + r.text + '</a>')
        }
        else if (window.getSelection) {
            r = window.getSelection();
            if (r.rangeCount > 0) {
                r = r.getRangeAt(0);
                var a = document.createElement('a');
                a.href = '#';
                r.surroundContents(a)
                r.collapse(false);
            }
        }
    }
</script>
</head>
js如何给选中的文字添加链接源代码如下<br>
js如何给选中的文字添加链接,就是选中的文字用a对象包裹起来。
 
<input type="button"  id="Button1" value="替换选中文字" onclick="surroundContent()"/>

 

加支付宝好友偷能量挖...


原创文章,转载请注明出处:js给选择文字添加链接

评论(0)Web开发网
阅读(334)喜欢(0)JavaScript/Ajax开发技巧