在弹出元素出现之前,是否有任何特定的方法来执行JS脚本,而不使用setTimeout()
?以下是弹出窗口的步骤。
我目前的实施情况如下。
$(document).on('click',function() {
window.setTimeout(function() {
if ($('#pop-up').css("visibility") === "visible") {
$('.modal-footer button').click(function() {
// Do something;
}
});
}
},1500);
});
答案 0 :(得分:0)
如果您正在显示bootstrap模式(因为我可以从您在代码中使用的boostrap类中推断出来),那么您可以使用shown.bs.modal
查看官方文档here
shown.bs.modal
当模态对用户可见时将触发此事件(将等待CSS过渡完成)。如果由单击引起,则单击的元素可用作事件的relatedTarget属性。
例如
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Modal Events - shown.bs.modal</h2>
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" id="myBtn">Open Modal</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>The <strong>shown.bs.modal</strong> event occurs when the modal is fully shown.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function(){
$("#myBtn").click(function(){
$("#myModal").modal("show");
});
$("#myModal").on('shown.bs.modal', function () {
alert('The modal is fully shown.');
});
});
</script>
</body>
</html>