检测 jQuery UI 对话框是否打开

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3313784/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:13:31  来源:igfitidea点击:

Detect if a jQuery UI dialog box is open

jqueryjquery-uijquery-ui-dialog

提问by user208662

I am using a jQuery UI dialog. If it is open, I want to do one thing. If it is closed, I want to do another.

我正在使用 jQuery UI 对话框。如果它是开放的,我想做一件事。如果它关闭了,我想做另一个。

My question is, how do I detect if a jQuery UI dialog box is open or not?

我的问题是,如何检测 jQuery UI 对话框是否打开?

回答by Byron Whitlock

If you read the docs.

如果您阅读文档。

$('#mydialog').dialog('isOpen')

This method returns a Boolean (true or false), not a jQuery object.

此方法返回一个布尔值(真或假),而不是一个 jQuery 对象。

回答by marcovtwout

Actually, you have to explicitly compare it to true. If the dialog doesn't exist yet, it will not return false (as you would expect), it will return a DOM object.

实际上,您必须明确将其与 true 进行比较。如果对话框尚不存在,它不会返回 false(如您所料),它将返回一个 DOM 对象。

if ($('#mydialog').dialog('isOpen') === true) {
    // true
} else {
    // false
}

回答by Nick Craver

If you want to check if the dialog's open on a particular element you can do this:

如果要检查对话框是否在特定元素上打开,可以执行以下操作:

if ($('#elem').closest('.ui-dialog').is(':visible')) { 
  // do something
}

Or if you just want to check if the element itself is visible you can do:

或者,如果您只想检查元素本身是否可见,您可以执行以下操作:

if ($('#elem').is(':visible')) { 
  // do something
}

Or...

或者...

if ($('#elem:visible').length) { 
  // do something
}

回答by Avinash

jQuery dialog has an isOpenproperty that can be used to check if a jQuery dialog is open or not.

jQuery 对话框有一个isOpen属性,可用于检查 jQuery 对话框是否打开。

You can see example at this link: http://www.codegateway.com/2012/02/detect-if-jquery-dialog-box-is-open.html

您可以在此链接中查看示例:http: //www.codegateway.com/2012/02/detect-if-jquery-dialog-box-is-open.html

回答by user2452922

Nick Craver's comment is the simplest to avoid the error that occurs if the dialog has not yet been defined:

Nick Craver 的评论是最简单的,以避免在对话框尚未定义时发生的错误:

if ($('#elem').is(':visible')) { 
  // do something
}

You should set visibility in your CSS first though, using simply:

不过,您应该先在 CSS 中设置可见性,只需使用:

#elem { display: none; }