我可以检查是否显示/隐藏Bootstrap模式?

问题描述 投票:60回答:9

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

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

我需要真/假

javascript twitter-bootstrap twitter-bootstrap-3 bootstrap-modal
9个回答
25
投票

最好的方法在文档中给出

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

欲了解更多信息,请参阅http://getbootstrap.com/javascript/#modals


20
投票

这是一个古老的问题,但无论如何,这是我用过的东西,因为某人正在寻找同样的事情

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

4
投票

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

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

3
投票

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

E.g:

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

3
投票

以官方方式:

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

{}用于避免模态尚未打开的情况(它返回undefined)。你也可以指定它等于{isShown: false},以保持它更有意义。


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

1
投票

使用Bootstrap 4:

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

1
投票

对我来说这很有效

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

alert("modal shown");

© www.soinside.com 2019 - 2024. All rights reserved.