2

我想将在父窗口中创建的对象附加到子窗口:

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

但新的 DIV 仅附加到子窗口。如果我评论最后一行 - div 将附加到父窗口。这能解决吗?

4

2 回答 2

3

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

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

新窗口中仍然没有可以附加到的 html 或正文。至少不是镀铬的。

试试这个:

<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>
于 2011-12-02T08:09:41.610 回答
1

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

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

clone = div.cloneNode(true);
render = window.open().document;
render.body.appendChild(clone);
于 2011-12-02T07:52:55.533 回答