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