Appending same node in different windows

孤街浪徒 提交于 2020-01-04 07:58:07

问题


I want to append an object, that was created in a parent window to a child window:

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

but the new DIV is appended only to the child window. If I comment the last line - the div is appended to the parent window. Can that be solved?


回答1:


Editing since I misread the question:

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

Still there is no html or body in the new window that it can be appended to. At least not in chrome.

Try this instead:

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



回答2:


Have you tried creating a copy of that div and then appending that to the child instead of the original div?

Edit: Okay, then yeah, that would be the cloneNode function.

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


来源:https://stackoverflow.com/questions/8349245/appending-same-node-in-different-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!