简体   繁体   English

可靠地隐藏Bootstrap模态

[英]Reliably hide Bootstrap modal

I'm using Bootstrap 2.3.2, and I'm using modal dialogs like this: 我正在使用Bootstrap 2.3.2,我正在使用这样的模态对话框:

<div id="notice1" class="modal hide fade">
    <div class="modal-body">
        <h4>This is a dialog for user...</h4>
    </div>
    ...
</div>

and

var notice1 = $("#notice1");
notice1.modal({
    keyboard: false,
    backdrop: "static",
    show: false
});

// Show the dialog
notice1.modal("show");

// Close the dialog
notice1.modal("hide");

Most of the the time, the above works fine and the modal dialog are opened and closed programmatically. 大多数情况下,上述工作正常,模式对话框以编程方式打开和关闭。 However, in some rare cases, calling .modal("hide") does not close the dialog at all though the dark backdrop is removed. 但是,在极少数情况下,调用.modal("hide")根本不会关闭对话框,尽管黑色背景被删除。

This is a huge potential issue because the dialog may get stuck on the screen and block part of the content. 这是一个巨大的潜在问题,因为对话框可能会卡在屏幕上并阻止部分内容。

Is there a reliable way to ensure the dialog is always closed after calling .modal("hide") ? 是否有一种可靠的方法可以确保在调用.modal("hide")后始终关闭对话框? Or better yet, how do we ensure a consistent hide behavior from Bootstrap ? 或者更好的是,我们如何确保Bootstrap的一致hide行为? I don't want to remove the dialog completely from the DOM, because the same dialog may be re-used on the page. 我不想完全从DOM中删除对话框,因为可以在页面上重复使用相同的对话框。

You can hide the modal by using the following code. 您可以使用以下代码隐藏模态。

 $("#notice1").hide();
 $(".modal-backdrop").hide();

According to documentation: http://getbootstrap.com/2.3.2/javascript.html#modals 根据文档: http//getbootstrap.com/2.3.2/javascript.html#modals

You can catch the hidden event and force the display:none property. 您可以捕获hidden事件并强制display:none属性。

    notice1.on('hidden', function () {
      $(this).css("display", "none")
    })

我正在使用1.9.x,以下代码工作..

 $("#yourModalWindow").modal('hide'); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM