从iframe内部关闭Bootstrap模式

Con*_*.FF 32 javascript jquery parent-child twitter-bootstrap

使用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>
Run Code Online (Sandbox Code Playgroud)

而我正在寻找一种从iframe内部关闭此模态的方法.防爆.单击内部的链接iframe-modal.html以关闭模态.我尝试了什么,但没有成功:

$('#iframeModal', window.parent.document).modal('hide');
$('#iframeModal', top.document).modal('hide');
$('#iframeModal').modal('hide');
Run Code Online (Sandbox Code Playgroud)

Dzu*_*sir 59

您始终可以创建一个全局可访问的函数来关闭Bootstrap模式窗口.

例如.

window.closeModal = function(){
    $('#iframeModal').modal('hide');
};
Run Code Online (Sandbox Code Playgroud)

然后从iframe中调用它:

window.parent.closeModal();
Run Code Online (Sandbox Code Playgroud)


Jos*_*hMB 12

问题是jQuery事件正在使用单个页面的jQuery实例注册.因此,$('#iframeModal', window.parent.document).modal('hide');实际上是要在iframe中触发hide事件,而不是父文档.

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

这将使用父的jQuery实例来查找项(因此不需要上下文),然后它将在父项中正确触发事件.