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

Sar*_*ara 5 javascript jquery twitter-bootstrap bootstrap-modal bootstrap-4

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

 $('#myModal').is(':visible');   
 $('#myModal').data('bs.modal').isShown ;        
 $('#myModal').hasClass('in');
Run Code Online (Sandbox Code Playgroud)

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

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

che*_*aby 7

当引导模式(V4中)表示,据我可以看到,三件事情发生在DOM,模态得到命名为两类褪色显示分别与样式属性显示(和mybe其他类似的填充...) 并且body标签获得一个名为modal-open的类。所以实际上你所要做的就是检查其中一件事情,我建议检查节目类的现有内容。

$('#exampleModal').hasClass('show'); // return true if the modal is open
Run Code Online (Sandbox Code Playgroud)

或者

$('body').hasClass('modal-open');
Run Code Online (Sandbox Code Playgroud)

$('#exampleModal').hasClass('show'); // return true if the modal is open
Run Code Online (Sandbox Code Playgroud)


Cal*_*lam 5

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