问题描述
我在的index.php以下code
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>
我在打开的fancybox的iframe,我期待用这个iframe中一个触发器img元素从image_group_1移动到image_group_2
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");
不过,由于触发是iframe中,我曾尝试没有成功以下
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?
推荐答案
假设两个父窗口和iframe的内容都在同一个域中,你可以尝试使用 window.parent
作为一个上下文选择。试试这个:
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);
如果域是不同的,那么你将不能够做到这一点,无论是使用jQuery的或本地的JS。
If the domains are different, then you will not be able to achieve this, using either jQuery or native JS.
这篇关于从母公司的iframe中移动元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!