jQuery UI对话框 – 检查是否存在按实例方法
我想使用instance方法来testing,如果jQuery UI对话框小部件已被初始化或没有。 关于API ,这是可能的,但它不适用于我: 
 Uncaught Error: cannot call methods on dialog prior to initialization; attempted to call method 'instance' 
演示: http : //jsfiddle.net/mDbV7/
更新:
 这是文档中的一个错误, instance方法将从1.11.0版本可用,请参阅此问题 。 
最新版本的jQuery UI不再允许您调用尚未初始化的项目的UI方法。 我刚刚把它们包装在if语句中,如:
 if ($("#divToBeDialoged").hasClass('ui-dialog-content')) { // do whatever } else { // it is not initialized yet } 
编辑:改变了类名,谢谢@dmnc
一旦完成对话,清空和销毁对话也是一个好习惯。 我通常在每个对话框的closures事件中使用这个代码
 $("#myDialog").dialog({ // other options close: function(event, ui) { $(this).empty().dialog('destroy'); } } 
这是我的build议,而不是每次都询问一个实例中是否存在对话框,确保每个对话框都清理完毕。
您可以使用:
 if($('#id').is(':ui-dialog')) { } 
要么
  var obj = $('<div>test</div>').dialog(); if (obj.is(':ui-dialog')) { alert('I\'ma dialog') } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css"> <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script> 
  if ($('#update').is(':data(dialog)')) { //#update has dialog } else { //#update does't have dialog } 
对于jQuery UI – v1.10.3
 if($( "#myDialog" ).is(':data(uiDialog)')){//is(':data(dialog)') does not work //Dialog exist } 
另一种方法是
 $('.element').is(':data(dialog)'); 
如果您正在使用HTML代码中的现有ID创build对话框,请使用下面的示例:
 $('#main').dialog({}); 
 请注意, dialog()将类ui-dialog添加到为其工作的生成的<div>父元素中。 在#main元素中, dialog()添加的类是: ui-dialog-content和ui-widget-content (在jquery-ui-1.9.2中)。 所以,在这种情况下,以@jbabey为例,你可以检查现有的对话框: 
 if ($('#main').hasClass('ui-dialog-content')) { // do whatever }