Home > Web Front-end > JS Tutorial > JavaScript adds hot word links

JavaScript adds hot word links

高洛峰
Release: 2016-11-25 14:27:24
Original
1180 people have browsed it

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title>添加超链接</title> 
</head> 
<body> 
    <div id="divContent"> 
        <p> 
            1、男人是哲学,女人是诗。没有诗的哲学是枯燥的,没有哲学的诗是肤浅的。哲学理性而诗感性,男人要想读懂诗,要先弄明白自己的哲学,女人想要理解哲学,就要先明白自己这首诗,深度的哲学只有配上适当韵味的诗才能共鸣,于是最好的不一定适合你,适合你的才是最好的。</p> 
    </div> 
    <br /> 
    <div id="divtest"> 
        <p> 
            2、飞雪连天射白鹿,笑书神侠倚碧鸳!</p> 
    </div> 
    <input type="button" onclick="javascript:test(event);" value="加链接" /> 
    <script> 
        function test(e) {//其他浏览器  
            if (e.target) { 
                var a = window.getSelection().getRangeAt(0); 
                var b = a.toString(); 
                var z = document.createElement("span"); 
                b = b.link("http://www.cecb2b.com/nic/" + a + ".html"); 
                z.innerHTML = b; 
                a.deleteContents(); 
                a.insertNode(z); 
            } 
            else { //ie浏览器  
                var text = document.getElementById("divContent").innerHTML; 
                var t = document.selection.createRange(); 
                t.pasteHTML("<a href="http://www.php1.cn/">html&#39;>" + t.text + "</a>"); 
            } 
        } 
    </script> 
</body> 
</html>
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template