Home  >  Article  >  Web Front-end  >  clone method of range object

clone method of range object

高洛峰
高洛峰Original
2017-02-09 14:37:311464browse

clone: ​​Clone of range object;

<body>
    <p id="p">这是一段文字</p>
    <button onclick="btn()">克隆</button>
    <script>
        function btn() {
            var p=document.getElementById("p");
            var rangeObj=document.createRange();
            rangeObj.selectNodeContents(p)
            var clone=rangeObj.cloneRange();
            alert(clone.toString());
        }
    </script>
</body>
<body>
    <div id="div">这是要克隆的文字内容<br/></div>
    <button onclick="btn()">克隆</button>
    <script>
        function btn() {
            var div=document.getElementById("div");
            var rangeObj=document.createRange();
            rangeObj.selectNodeContents(div);
            var clone=rangeObj.cloneContents();
            //appendChild() 方法向节点添加最后一个子节点。
            div.appendChild(clone);
        }
    </script>
</body>

For more articles related to the clone method of range object, please pay attention to the PHP Chinese website!

Statement:
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