Javascript jQuery 如何在对话框内从 iframe 关闭对话框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3575955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery how to close dialog from iframe within dialog?
提问by JD Isaacks
If I open a dialog like so:
如果我像这样打开一个对话框:
$('<iframe id="externalSite" class="externalSite" src="http://www.example.com" />').dialog({
autoOpen: true,
width: 800,
height: 500,
modal: true,
resizable: true
})
How can I close the dialog from withing the iframe?
如何使用 iframe 关闭对话框?
回答by JD Isaacks
OK so I put the iframe on the page with display set to none. I open it like this:
好的,所以我将 iframe 放在显示设置为无的页面上。我是这样打开的:
$('#externalSite').dialog({ ... });
on the main parent window I have a function like this:
在主父窗口上,我有一个这样的功能:
function closeIframe()
{
$('#externalSite').dialog('close');
return false;
}
From within the iframe I call:
我从 iframe 中调用:
window.parent.closeIframe();
回答by Kraftwurm
Simply calling the following worked for me:
简单地调用以下对我有用:
window.parent.$('#externalSite').dialog('close');
window.parent.$('#externalSite').dialog('close');
回答by James Sumners
Have you tried this?:
你试过这个吗?:
$(window.parent).dialog('close');
I have never used the jQuery UI dialog, so I'm not sure that will actually work. It seems to me that you would need to maintain a reference to the dialog you created so that you can use it to close the dialog.
我从未使用过 jQuery UI 对话框,所以我不确定它是否真的有效。在我看来,您需要维护对您创建的对话框的引用,以便您可以使用它来关闭对话框。
Note, you could also look for elements in the parent's DOM by:
请注意,您还可以通过以下方式在父级的 DOM 中查找元素:
$('#someParentDOMElement' , window.parent);
Of course, all of this is assuming that the site you load within the iframeis on the same domain as the parent document. If not, then the document in your iframewill not have access to the parent DOM at all.
当然,所有这一切都假设您加载的站点iframe与父文档位于同一域中。如果没有,那么您中的文档iframe将根本无法访问父 DOM。

