gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:21:10 26 4
gpt4 key购买 nike

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

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

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

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

最佳答案

当 Bootstrap 模式 (v4) 被显示时,据我所知,DOM 中发生了三件事,该模式有两个名为 fade 的类> 和 show,以及带有 display 的样式属性以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>

关于javascript - 如何检查 bootstrap-4 模式是否已经打开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46998081/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com