我想追加一個對象相同的節點,在父窗口創建一個子窗口:追加在不同的窗口
div = document.createElement("div");
document.body.appendChild(div);
// Here come div's atts;
render = window.open().document;
render.body.appendChild(div);
但新DIV附加僅對子窗口。如果我評論最後一行 - div被附加到父窗口。這能解決嗎?
我想追加一個對象相同的節點,在父窗口創建一個子窗口:追加在不同的窗口
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或身體。至少不是在鉻。
試試這個:
<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?
編輯:好的,那麼是的,那就是cloneNode函數。
clone = div.cloneNode(true);
render = window.open().document;
render.body.appendChild(clone);
這就是我一直在尋找。有沒有一個函數(不是jQuery)來克隆元素?問題是我在兩個窗口中都需要完全相同的節點樹。 –
請仔細閱讀...... div首先附加到父窗口,但隨後立即附加到子窗口。你甚至可以看到「跳轉」 –
是的,我看到......你需要克隆它......和JavaScript一樣。你正在改變這個對象。 – Asken