简体   繁体   English

从iframe内部关闭Bootstrap模式

[英]Close Bootstrap modal from inside iframe

Page which opens Twitter Bootstrap Modal with iframe inside: 使用iframe打开Twitter Bootstrap Modal的页面:

<div id="iframeModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-header">
        <button type="button" class="btn btn-danger pull-right" data-dismiss="modal" aria-hidden="true">Close</button>
        <div class="clearfix"></div>
    </div>
    <div class="modal-body">
        <iframe src="iframe-modal.html"></iframe> 
    </div>
    <div class="modal-footer">
    </div>
</div>

And I am looking for a way to close this modal from inside the iframe. 而我正在寻找一种从iframe内部关闭此模态的方法。 Ex. 防爆。 clicking a link inside the iframe-modal.html to close the modal. 单击iframe-modal.html内的链接以关闭模式。 What I have tried, but non successful: 我尝试了什么,但没有成功:

$('#iframeModal', window.parent.document).modal('hide');
$('#iframeModal', top.document).modal('hide');
$('#iframeModal').modal('hide');

You can always create a globally accessible function which closes the Bootstrap modal window. 您始终可以创建一个全局可访问的函数来关闭Bootstrap模式窗口。

eg. 例如。

window.closeModal = function(){
    $('#iframeModal').modal('hide');
};

Then from the iframe, call it using: 然后从iframe中调用它:

window.parent.closeModal();

The issue is that jQuery events are being registered with the individual page's instance of jQuery. 问题是jQuery事件正在使用单个页面的jQuery实例注册。 So, $('#iframeModal', window.parent.document).modal('hide'); 所以, $('#iframeModal', window.parent.document).modal('hide'); is actually going to trigger the hide event in the iframe, not the parent document. 实际上是要在iframe中触发hide事件,而不是父文档。

This should work: parent.$('#iframeModal').modal('hide'); 这应该工作: parent.$('#iframeModal').modal('hide');

This will use the parent's instance of jQuery to find the item (so no context is needed), and it will then fire the event correctly in the parent. 这将使用父的jQuery实例来查找项(因此不需要上下文),然后它将在父项中正确触发事件。

One more solution in case you don't know id of modal which use iframe : 如果你不知道使用iframe的模态的id ,还有一个解决方案:

Add function CloseModal 添加函数CloseModal

function CloseModal(frameElement) {
     if (frameElement) {
        var dialog = $(frameElement).closest(".modal");
        if (dialog.length > 0) {
            dialog.modal("hide");
        }
     }
}

where frameElement is reference to iframe element container. 其中frameElementiframe元素容器的引用。

And this parameter can be passed from iframe like so: 这个参数可以从iframe传递,如下所示:

window.parent.CloseModal(window.frameElement);

More about window.frameElement you can find here 有关window.frameElement更多信息,请点击此处

您也可以触发单击关闭按钮:

$('#iframeModal button.mce-close', parent.document).trigger('click');

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

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