简体   繁体   English

JavaScript DOM:框架之间的传输元素

[英]JavaScript DOM: transfer element between frames

I'm using the following jQuery code within an iframe (same origin) to try to move a node from the iframe to the main (top) document. 我正在iframe(相同来源)中使用以下jQuery代码,以尝试将节点从iframe移到主(顶部)文档。

var dest = $(window.top.document).find('#dest_id');
dest.append($('#source_id'));

The following works ok: 以下工作正常:

window.top.document.getElementById('dest_id').innerHTML = document.getElementById('source_id').innerHTML;

I guess there is a limitation in moving DOM nodes between frames. 我猜想在帧之间移动DOM节点有一个限制。 Is there any way around this either in plain js or in jquery? 在纯js或jquery中有没有解决办法?

Edit: Could it be something to do with the ownerDocument property? 编辑:可能与ownerDocument属性有关? I tried changing it to window.top.document on #dest_id but it didn't work. 我尝试将其更改为window.top.document上的window.top.document,但是没有用。

Edit, similar question: Can't appendChild to a node created from another frame 编辑,类似的问题: 不能将Child追加到从另一个框架创建的节点上

It's a bad idea to attempt to move elements between frames, because there's no way you can enforce your user to only view the page attempting to send data in the context of an iframe - the user could choose to navigate directly to the page. 尝试在框架之间移动元素是一个坏主意,因为您无法强制用户仅在iframe的上下文中查看试图发送数据的页面-用户可以选择直接导航至页面。 Out of its original context, the page will just fail to work properly. 超出其原始上下文,该页面将无法正常工作。

A much better solution is to make the child page write the data you want the parent page to receive into a database or some other kind of persistent session data, You could then use some kind of callback to signal the parent page the correct time to check the session data for your returned data. 更好的解决方案是使子页面将您希望父页面接收的数据写入数据库或其他某种持久性会话数据,然后可以使用某种回调来通知父页面正确的检查时间您返回的数据的会话数据。 This is much neater, and doesn't rely on your pages being viewed in certain contexts. 这更加整洁,并且不依赖于在特定上下文中查看您的页面。

Not a direct response to your question, I know. 我知道,这不是直接回答您的问题。 But there's my opinion :-) 但是我有意见:-)

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM