如何检查bootstrap-4模式是否已打开?

时间:2017-10-29 08:08:07

标签: javascript jquery twitter-bootstrap bootstrap-modal bootstrap-4

我想检查某个模态是否已经打开,我已经尝试过这种方法(bootstrap-4)但是对我来说不起作用(即使对话框打开也总是给'假')

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

我检查了模态classList,在屏幕可见之后没有任何新类添加到我的模态中

我不想要事件处理程序,因为我不想跟踪状态,我只想检查对话框以前是否通过其他功能打开

2 个答案:

答案 0 :(得分:2)

如果通过Bootstrap JS显示模态,你的模态将具有类.show,所以如果你想看模态当前是否打开,你可以检查$('#myModal').hasClass('show')

答案 1 :(得分:1)

当一个bootstrap模态(v4显示时,据我所知,DOM中发生了三件事,modal获得了两个名为 fade <的类/ strong>和显示,以及显示阻止的样式属性(mybe其他人如 padding 。 ..)和 body 标签获得一个名为 modal-open 的类。所以你要做的就是检查其中一件事,我建议检查现有的 show 类。

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

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

&#13;
&#13;
<!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>
&#13;
&#13;
&#13;