[英]Can I move a DOM node from an iFrame to parent's body?
Currently I'm trying to move a DOM node by using the following snippet: 目前,我正在尝试使用以下代码段移动DOM节点:
var element = document.getElementById('my-id');
parent.window.body.appendChild(element);
The code above is rendered within an iFrame, but I'm getting the exception "parent.window.body is undefined". 上面的代码在iFrame中呈现,但出现了“ parent.window.body未定义”异常。 Is this a security feature (since I effectively do want to break out of the iframe)? 这是一项安全功能(因为我实际上确实想退出iframe)?
Please note: I do not want to re-ask "how to move a DOM node" per se , but to move it across borders of an iframe! 请注意: 我不想再问“如何移动DOM节点”本身 ,而是要跨iframe的边界移动它!
Yes, you can, within the same origin . 是的,您可以在同一来源内 。 (Checked on Firefox, Chrome, IE8, IE11, and Edge.) Note that when you do so, any event listeners attached to it remain attached, which could be confusing. (在Firefox,Chrome,IE8,IE11和Edge上进行了检查。)请注意,这样做时,附加到它的所有事件侦听器都保持附加状态,这可能会造成混淆。 Also note that the old DOM3 spec allowed for an error if the node was being appended to a different document, but the more recent WHAT-WG DOM spec doesn't mention doing that (if you start at appendChild
and follow through, you end up here , which doesn't have that exception listed). 还要注意,如果将节点追加到其他文档中,则旧的DOM3规范会导致错误,但是较新的WHAT-WG DOM规范没有提及这样做(如果您从appendChild
开始并遵循,则最终这里 ,没有列出该异常)。
But body
isn't a property of the window, it's a property of the document
. 但是body
不是窗口的属性,而是document
的属性。 Also note that parent.window
is redundant. 还要注意, parent.window
是多余的。 So: 所以:
parent.document.body.appendChild(element);
// ----^
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.