twitter-bootstrap 我可以检查 Bootstrap Modal 是否显示/隐藏?

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

Can I check if Bootstrap Modal Shown / Hidden?

javascripttwitter-bootstraptwitter-bootstrap-3bootstrap-modal

提问by user2736812

Can I check if Bootstrap Modal currently Shown / Hidden Programatically?

我可以检查 Bootstrap Modal 当前是否以编程方式显示/隐藏吗?

Like bool a = if("#myModal").shown();?

喜欢 bool a = if("#myModal").shown();

I need true/false

我需要真/假

回答by

alert($('#myModal').hasClass('in'));

It will return true if modal is open

如果模态打开,它将返回真

回答by vipulsharma

The best method is given in the docs

文档中给出了最好的方法

$('#myModal').on('shown.bs.modal', function () {
  // will only come inside after the modal is shown
});

for more info refer http://getbootstrap.com/javascript/#modals

有关更多信息,请参阅http://getbootstrap.com/javascript/#modals

回答by ctf0

its an old question but anyway heres something i used incase someone was looking for the same thing

这是一个老问题,但无论如何我使用了一些东西,以防有人在寻找同样的东西

if (!$('#myModal').is(':visible')) {
    // if modal is not shown/visible then do something
}

回答by Kamlesh

When modal hide? we check like this :

当模态隐藏?我们这样检查:

$('.yourmodal').on('hidden.bs.modal', function () {
    // do something here
})

回答by Pehlaj

Use hasClass('in'). It will return true if modal is in OPENstate.

使用hasClass('in'). 如果模态处于OPEN状态,它将返回真。

E.g:

例如:

if($('.modal').hasClass('in')){
   //Do something here
}

回答by Sakata Gintoki

In offical way:

以官方方式:

> ($("element").data('bs.modal') || {})._isShown    // Bootstrap 4
> ($("element").data('bs.modal') || {}).isShown     // Bootstrap <= 3

{}is used to avoid the case that modal is not opened yet (it return undefined). You can also assign it equal {isShown: false}to keep it's more make sense.

{}用于避免模态尚未打开的情况(它返回undefined)。您也可以将其分配为相等{isShown: false}以使其更有意义。

回答by NaturalBornCamper

With Bootstrap 4:

使用引导程序 4:

if ($('#myModal').hasClass('show')) {
    alert("Modal is visible")
}

回答by estani

All Bootstrap versions:

所有 Bootstrap 版本:

var isShown = $('.modal').hasClass('in') || $('.modal').hasClass('show')

To just close it independent of state and version:

独立于状态和版本关闭它:

$('.modal button.close').click()


more info

更多信息

Bootstrap 3 and before

Bootstrap 3 及之前

var isShown = $('.modal').hasClass('in')

Bootstrap 4

引导程序 4

var isShown = $('.modal').hasClass('show')

回答by Mónica Cifuentes

For me this works

对我来说这有效

 
if($("#myModal").css("display") !='none' && $("#myModal").css("visibility") != 'hidden')

alert("modal shown");

回答by Jaykishan

if($('.modal').hasClass('in')) {
    alert($('.modal .in').attr('id')); //ID of the opened modal
} else {
    alert("No pop-up opened");
}