Javascript 关闭所有打开的对话框?(JQuery)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13587159/
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
Close all open dialog boxes? (JQuery)
提问by netdjw
How can I close all opened dialogboxes in jQuery?
The situation is next: I have a simple page without dialogs. It has some buttons what open it owns dialogs.
如何关闭所有打开的dialog盒子jQuery?接下来的情况是:我有一个没有对话框的简单页面。它有一些按钮可以打开它拥有的对话框。
When I click on a button I need to close all opened dialogs.
当我点击一个按钮时,我需要关闭所有打开的对话框。
Here is the HTML:
这是 HTML:
<div id="buttons">
<a href="#" id="btn_1">Button 1</a>
<a href="#" id="btn_2">Button 2</a>
<a href="#" id="btn_3">Button 3</a>
</div>
<div id="dialog_1" class="dialogbox">...</div>
<div id="dialog_2" class="dialogbox">...</div>
<div id="dialog_3" class="dialogbox">...</div>
And here is the jQuery:
这是jQuery:
$(function() {
$('#buttons').find('a').click(function() {
// close all dialogs
$('.dialogbox').dialog("close");
// find out clicked id and open dialog
var nr = this.id.split("_")[1];
$('#dialog_'+nr).dialog();
});
});
The Chrome say: Uncaught Error: cannot call methods on dialog prior initialization; attempted to call method 'close'.
Chrome 说:Uncaught Error: cannot call methods on dialog prior initialization; attempted to call method 'close'.
I was tried to check $('.dialogbox').dialog('isOpen'), but same result.
我试图检查$('.dialogbox').dialog('isOpen'),但结果相同。
How can I close all dialogs?
如何关闭所有对话框?
回答by dsgriffin
Since they all inherit the same class, this is the best way to select all and close by:
由于它们都继承同一个类,因此这是选择全部并关闭的最佳方法:
$(".ui-dialog-content").dialog("close");
$(".ui-dialog-content").dialog("close");
回答by Rahul Tripathi
You can simple try this as they all have the .ui-dialog-contentclass, so select by that and close them, like this:-
你可以简单地试试这个,因为它们都有这个.ui-dialog-content类,所以选择它并关闭它们,就像这样:-
$(".ui-dialog-content").dialog("close");

