我可以检查Bootstrap Modal当前是否以编程方式显示/隐藏?
像bool a = if("#myModal").shown();
?
我需要真/假
最好的方法在文档中给出
$('#myModal').on('shown.bs.modal', function () {
// will only come inside after the modal is shown
});
欲了解更多信息,请参阅http://getbootstrap.com/javascript/#modals
这是一个古老的问题,但无论如何,这是我用过的东西,因为某人正在寻找同样的事情
if (!$('#myModal').is(':visible')) {
// if modal is not shown/visible then do something
}
模态隐藏?我们这样检查:
$('.yourmodal').on('hidden.bs.modal', function () {
// do something here
})
使用hasClass('in')
。如果modal处于OPEN
状态,它将返回true。
E.g:
if($('.modal').hasClass('in')){
//Do something here
}
以官方方式:
> ($("element").data('bs.modal') || {})._isShown // Bootstrap 4
> ($("element").data('bs.modal') || {}).isShown // Bootstrap <= 3
{}
用于避免模态尚未打开的情况(它返回undefined
)。你也可以指定它等于{isShown: false}
,以保持它更有意义。
if($('.modal').hasClass('in')) {
alert($('.modal .in').attr('id')); //ID of the opened modal
} else {
alert("No pop-up opened");
}
使用Bootstrap 4:
if ($('#myModal').hasClass('show')) {
alert("Modal is visible")
}
对我来说这很有效
if($("#myModal").css("display") !='none' && $("#myModal").css("visibility") != 'hidden')
alert("modal shown");