【发布时间】:2013-11-09 13:56:23
【问题描述】:
我可以检查引导模式当前是否以编程方式显示/隐藏?
喜欢bool a = if("#myModal").shown();?
我需要真/假
【问题讨论】:
标签: javascript twitter-bootstrap twitter-bootstrap-3 bootstrap-modal
我可以检查引导模式当前是否以编程方式显示/隐藏?
喜欢bool a = if("#myModal").shown();?
我需要真/假
【问题讨论】:
标签: javascript twitter-bootstrap twitter-bootstrap-3 bootstrap-modal
alert($('#myModal').hasClass('in'));
模态打开时返回true
【讨论】:
$('#myModal').hasClass('show') 有效,Bootstrap V4
if($('.modal.in, .modal.show').length) 获取 Bootstrap V2、3、4+
文档中给出了最好的方法
$('#myModal').on('shown.bs.modal', function () {
// will only come inside after the modal is shown
});
【讨论】:
这是一个老问题,但无论如何,我用了一些东西,以防有人在寻找同样的东西
if (!$('#myModal').is(':visible')) {
// if modal is not shown/visible then do something
}
【讨论】:
模态何时隐藏?我们这样检查:
$('.yourmodal').on('hidden.bs.modal', function () {
// do something here
})
【讨论】:
所有 Bootstrap 版本:
var isShown = $('.modal').hasClass('in') || $('.modal').hasClass('show')
关闭它独立于状态和版本:
$('.modal button.close').click()
Bootstrap 3 及之前的版本
var isShown = $('.modal').hasClass('in')
引导程序 4
var isShown = $('.modal').hasClass('show')
【讨论】:
使用hasClass('in')。如果modal处于OPEN状态,它将返回true。
例如:
if($('.modal').hasClass('in')){
//Do something here
}
【讨论】:
官方方式:
> ($("element").data('bs.modal') || {})._isShown // Bootstrap 4
> ($("element").data('bs.modal') || {}).isShown // Bootstrap <= 3
{} 用于避免模态尚未打开的情况(它返回undefined)。您还可以将其分配为等于 {isShown: false} 以使其更有意义。
【讨论】:
true,但在应用 shown 类之前
下面是一些自定义代码,可以为模态状态提供更明确命名的类:
$('.modal').on('show.bs.modal', function(e)
{
e.currentTarget.classList.add("modal-fading-in");
e.currentTarget.classList.remove("modal-fading-out");
e.currentTarget.classList.remove("modal-hidden");
e.currentTarget.classList.remove("modal-visible");
});
$('.modal').on('hide.bs.modal', function(e)
{
e.currentTarget.classList.add("modal-fading-out");
e.currentTarget.classList.remove("modal-fading-in");
e.currentTarget.classList.remove("modal-hidden");
e.currentTarget.classList.remove("modal-visible");
});
$('.modal').on('hidden.bs.modal', function(e)
{
e.currentTarget.classList.add("modal-hidden");
e.currentTarget.classList.remove("modal-fading-in");
e.currentTarget.classList.remove("modal-fading-out");
e.currentTarget.classList.remove("modal-visible");
});
$('.modal').on('shown.bs.modal', function(e)
{
e.currentTarget.classList.add("modal-visible");
e.currentTarget.classList.remove("modal-fading-in");
e.currentTarget.classList.remove("modal-fading-out");
e.currentTarget.classList.remove("modal-hidden");
});
然后,您可以使用 JS 和 CSS 轻松定位模态的各种状态。
JS 示例:
if (document.getElementById('myModal').hasClass('modal-fading-in'))
{
console.log("The modal is currently fading in. Please wait.");
}
CSS 示例:
.modal-fading-out, .modal-hidden
{
opacity: 0.5;
}
【讨论】:
使用 Bootstrap 4:
if ($('#myModal').hasClass('show')) {
alert("Modal is visible")
}
【讨论】:
if($('.modal').hasClass('in')) {
alert($('.modal .in').attr('id')); //ID of the opened modal
} else {
alert("No pop-up opened");
}
【讨论】:
对我来说这是可行的
if($("#myModal").css("display") !='none' && $("#myModal").css("visibility") != 'hidden')alert("modal shown");
【讨论】:
我尝试使用这样的函数,然后在需要时调用此函数。一直为我工作。
function modal_fix() {
var a = $(".modal"),
b = $("body");
a.on("shown.bs.modal", function () {
b.hasClass("modal-open") || b.addClass("modal-open");
});
}
【讨论】:
这解决了你的问题,如果 true 不刷新,false refresh
var truFalse = $('body').hasClass('modal-open');
【讨论】: