从iframe中选择iframe的直接父元素 [英] select the immediate parent element of an iframe from the iframe
问题描述
我有以下标记:
<html>
<body>
<div class="aaa"></div>
<div class="aaa"></div>
<div class="aaa">
<iframe>
<form id="myform">
</form>
</iframe>
</div>
<div class="aaa"></div>
</body>
</html>
在加载iframe
时,它包含形式"#myform".
提交表单后,我需要基于div.aaa
(它是iframe
的直接父级)运行某个回调.
On loading the iframe
, it contains the form '#myform'.
Upon submission of the form, I need to run a certain callback based on the div.aaa
that is the immediate parent of the iframe
.
任何基于parent.document
的解决方案都帮不了我大忙,因为我不知道div.aaa
的ID.
Any solution based on parent.document
cannot help me much, since I do not know the ID of the div.aaa
.
我想做的事情类似于$('#myform').closest('div.aaa')
,但是,这当然会失败,因为iframe
窗口本身之外的任何元素都无法以这种方式访问.
What I'd like to do is something like $('#myform').closest('div.aaa')
, but, of course, this fails because any element beyond the iframe
window itself cannot be accessed in this fashion.
如何最好地访问此iframe的直接父级div
,它实际上是其父级文档中的一个元素?
How can I best access this iframe's immediate parent div
, which is actually an element in it's parent document?
parent
和iframe
的起源相同.
推荐答案
您可以跳到父窗口,并通过将框架元素contentWindow与iframe中的已知窗口进行匹配来在父文档中查找特定的iframe
You can jump up to the parent window and look for specific iframe in parent document by matching frame element contentWindow to known window inside the iframe
类似的东西:
$(window.parent.document).find('iframe').filter(function() {
return this.contentWindow === window;
}).parent().doSomething()
这篇关于从iframe中选择iframe的直接父元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!