如何检查bootstrap-4模态是否已打开? [英] how to check if bootstrap-4 modal already open?

查看:44
本文介绍了如何检查bootstrap-4模态是否已打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to check if a certain modal is already open , I have tried this methods (bootstrap-4) but didn't work for me (always give 'false' even if the dialog is open)

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

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

I checked modal classList , no any new class added to my modal after it became visible to the screen

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

I don't want event handlers as I don't want to track the status , I just want to check if the dialog was previously open via some other function

推荐答案

引导程序模式(v4 )是显示,据我所知,DOM中发生了三件事,该模式获得了两个名为 fade show的类.,以及带有 display block 的样式属性(以及我的其他人,例如 padding ...)和body 标签获得一个名为 modal-open 的类.因此,您真正要做的就是检查其中一项内容,我建议您检查 show 类的现有内容.

when a bootstrap modal (v4) is shown, as far as i can notice, three things happens in the DOM, the modal gets a two classes named fade and show respectively, and a style property with display to block (and mybe others like padding...) and the body tag gets a class named modal-open. So realy all you have to do is to check for one of those things, i recommend to check the existing of the show class.

$('#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>

这篇关于如何检查bootstrap-4模态是否已打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆