在不同的窗口中追加相同的节点

Appending same node in different windows

本文关键字:节点 追加 窗口      更新时间:2023-09-26

我想添加一个在父窗口创建的对象到子窗口:

div = document.createElement( "div" );
document.body.appendChild( div );
// Here come div's atts;
render = window.open().document;
render.body.appendChild( div );

,但是新的DIV只被附加到子窗口。如果对最后一行进行注释,则div将追加到父窗口。这个问题能解决吗?

编辑,因为我误读了问题:

newelement = element.cloneNode(true); // true to clone children too

在新窗口中仍然没有可以附加的html或正文。至少在chrome中没有。

试试这个:

<html>
<body>
    <script>
        div = document.createElement( "div" );
        // add some text to know if it's really there
        div.innerText = 'text of the div';
        document.body.appendChild( div );
        // Here come div's atts;
        render = window.open().document;
        // create the body of the new document
        render.write('<html><body></body></html>');
        // now you can append the div
        render.body.appendChild( div );
        alert(render.body.innerHTML);
    </script>
</body>
</html>

您是否尝试创建该div的副本,然后将其附加到子div而不是原始div?

编辑:好的,那么是的,这将是cloneNode函数。

clone = div.cloneNode(true);
render = window.open().document;
render.body.appendChild(clone);