如何检查Bootstrap-4模态框是否已经打开?

7

我想检查某个模态框是否已经打开,我尝试了以下方法(bootstrap-4),但对我来说都没有用(即使对话框已经打开,它始终返回“false”)

 $('#myModal').is(':visible');   
 $('#myModal').data('bs.modal').isShown ;        
 $('#myModal').hasClass('in');

我检查了弹出框的类列表,当它可见于屏幕后,没有添加任何新的类。

我不想使用事件处理程序,因为我不想追踪状态,我只是想检查对话框是否以前通过某些其他函数打开过。

2个回答

8

当一个bootstrap模态框(v4)被展示时,在DOM中会发生三件事情:模态框分别获取两个名为fadeshow的类,一个样式属性以block的方式显示(也许还包括其他属性如padding...),body标签获取一个名为modal-open的类。因此,你只需要检查其中一个即可,我建议检查show类是否存在。

$('#exampleModal').hasClass('show'); // return true if the modal is open

或者

$('body').hasClass('modal-open');

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="x-ua-compatible" content="ie=edge">
        <title>Bootstrap modal</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
        <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>
    </head>
    <body>

      <!-- Modal -->
      <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
          <div class="modal-content">
            <div class="modal-header">
              <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
              <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
              </button>
            </div>
            <div class="modal-body">
              ...
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
              <button type="button" class="btn btn-primary">Save changes</button>
            </div>
          </div>
        </div>
      </div>
      <script>
      
           $('#myModal').modal('show');
           
           setTimeout(function() {
              
              var isShown = $('#myModal').hasClass('show');
              console.log(isShown);
              
           }, 1000);
      
      </script>
    </body>
</html>


当对话框首次启动时,没有添加任何额外的类。 - Sara
setTimeout 已经起作用了 (Y),但是是否有另一种解决方案,而不依赖于这个 timeout 值(即:1000ms)? - Sara
@Sara 我认为这可能取决于你的脚本是如何/何时执行的。 - chebaby
更多细节请。 - Sara
这是来自 Bootstrap 示例的标准模态代码,没有任何更改 @chebaby - Sara
@Sara,我有点迷失了……Bootstrap的示例运行得很好!!我想你可能要问的是模态框事件!!https://v4-alpha.getbootstrap.com/components/modal/#events - chebaby

6

如果使用Bootstrap JS显示了模态框,该模态框将具有类.show。因此,如果您想查看模态框当前是否已打开,则可以检查$('#myModal').hasClass('show')


打开对话框时也返回了false。 - Sara
当您检查$('#myModal')时,会得到什么?请向我们展示结果。 - Callam
我检查了模态框的classList,发现在它显示在屏幕上后没有添加任何新的类。 - Sara
它仅返回纯文本。 - Sara
这是什么纯文本?你只输入了 $('#myModal') 吗? - Callam
$('#myModal').modal(); $('#myModal .modal-body').html('测试'); - Sara

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接