我怎样才能使这种模态持久化? [英] How can I make this modal persistent?
问题描述
我正在寻找一种方法,使这种模式一旦显示就可以持久.如此处所示,用户可以在div外部单击一下即可将其关闭.
I am looking for a way to make this modal persistent once it show up. As it's presented here, the user can close it with a simple click outside of the div.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Activate Modal with JavaScript</h2>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function(){
$(window).load(function(){
$('#myModal').modal('show');
});
});
</script>
</body>
</html>
是否有一种方法可以阻止此模式,因此即使在其外部单击鼠标,该模式仍然存在?
Is there a way to block this modal so it's still there even with a mouse click outside of it?
推荐答案
要将持久模式"(也称为隐藏")挂钩到hide事件中,并返回false以防止隐藏.
To "persist the modal" (aka prevent hiding) hook into the hide event and return false to prevent hiding.
$("#myModal").on('hide.bs.modal', function () {
return false
});
这是完整的代码示例.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Persistent Modal</h2>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">My Persistent Modal</h4>
</div>
<div class="modal-body">
<p>I'm here for good. Can't hide me.</p>
</div>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function(){
$(window).load(function(){
$('#myModal').modal('show');
});
$("#myModal").on('hide.bs.modal', function () {
return false
});
});
</script>
</body>
</html>
编辑:要隐藏模式,请在调用hide()
之前重新定义对hide事件的响应.以下功能可以做到这一点.您可以在适当的时候(例如单击按钮)执行该功能.
Edit: To hide the modal, redefine the response to the hide event before calling hide()
. The following function does this. You can execute the function whenever is appropriate (e.g. on a button click).
function hideMyModal () {
$("#myModal").on('hide.bs.modal', function () { });
$("#myModal").hide()
}
这篇关于我怎样才能使这种模态持久化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!