Move element within parent from iframe

蓝咒 提交于 2019-12-11 13:05:09

问题


I have the following code in index.php

<div id="image_group_1">
    <img src="/images/image.jpg" id="image1" data-temp="Some useful text">
</div>
<div id="image_group_2">
</div>

I have an iframe that opens in Fancybox and I am looking to use a trigger within this iframe to move the img element from image_group_1 to image_group_2

Now, if this were being triggered within the main document then this would be a matter of

$("#image1").appendTo("#image_group_2");

However, as the trigger is within the iframe, I have tried the following without success

var moveFrom = window.parent.$("#image1");
var moveTo = window.parent.$("#image_group_2");
moveFrom.appendTo(moveTo);

Any suggestions as to how to get the desired result?


回答1:


Assuming both the parent window and iframe content are on the same domain, you can try using the window.parent as a context selector. Try this:

var moveFrom = $("#image1", window.parent.document);
var moveTo = $("#image_group_2", window.parent.document);
moveFrom.appendTo(moveTo);

If the domains are different, then you will not be able to achieve this, using either jQuery or native JS.



来源:https://stackoverflow.com/questions/18299020/move-element-within-parent-from-iframe

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