如果要将内容设置为Bootstrap模态,请使用.modal(options)方法。
为此,请使用jQuery在单击按钮时设置模型,如以下代码片段所示-
$(document).ready(function(){ $("#button1").click(function(){ $("#newModal1").modal({backdrop: true}); }); });
现在让我们实现modal(“ options”)类。在这里,我们有两个按钮,可在单击时生成不同类型的模态。其中一个将在模式外部单击以关闭,而另一个将不会关闭。
这是完整的例子-
<!DOCTYPE html> <html> <head> <title>Bootstrap Example</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://cdn.staticfile.org/jquery/3.3.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>Sample</h2> <button type="button" class="btn btn-default btn-lg" id="button1">Modal One</button> <button type="button" class="btn btn-default btn-lg" id="button2">Modal Two</button> <div class="modal fade" id="newModal1" role="dialog"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">×</button> <h4 class="modal-title">Sample Modal 1</h4> </div> <div class="modal-body"> <p>Click outside to close it.</p> </div> <div class="modal-footer"> <button type="button" class="btn btn-primary" data-dismiss="modal">Close</button> </div> </div> </div> </div> <div class="modal fade" id="newModal2" role="dialog"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">×</button> <h4 class="modal-title">Sample Modal 2</h4> </div> <div class="modal-body"> <p>It won't close on clicking outside.</p> </div> <div class="modal-footer"> <button type="button" class="btn btn-primary" data-dismiss="modal">Close</button> </div> </div> </div> </div> </div> <script> $(document).ready(function(){ $("#button1").click(function(){ $("#newModal1").modal({backdrop: true}); }); $("#button2").click(function(){ $("#newModal2").modal({backdrop: false}); }); }); </script> </body> </html>