JavaScript Tutorial/DOM Node/clone

Материал из Web эксперт
Перейти к: навигация, поиск

Clone contents of a range

This example only works in browsers that support DOM ranges. 
This example will fail in Internet Explorer.
<html>
    <head>
        <title>DOM Range Example</title>
        <script type="text/javascript">
            function cloneContent() {
                var oP1 = document.getElementById("p1");
                var oHello = oP1.firstChild.firstChild;
                var oWorld = oP1.lastChild;
                var oRange = document.createRange();
                
                oRange.setStart(oHello, 2);
                oRange.setEnd(oWorld, 3);
                var oFragment = oRange.cloneContents();
                
                document.body.appendChild(oFragment);
                                
            }
        </script>
    </head>
    <body><p id="p1"><b>Hello</b> World</p>
        <input type="button" value="Clone Content" onclick="cloneContent()" />   
     
    </body>
</html>