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 に答える