Bootstrap show.bs.modal 事件
Bootstrap 中的 show.bs.modal 事件在即将显示模态框时触发。
此处使用相同的事件设置提醒,当模型准备就绪时,事件触发。这会导致向访问者显示提醒。
基于此,我还设置了一个按钮,单击该按钮即可打开模态框,如下面的代码段所示: −
$(document).ready(function(){ $("#button1").click(function(){ $("#newModal").modal("show"); }); $("#newModal").on('show.bs.modal', function () { alert('The modal will be displayed now!'); }); });
以下是 Bootstrap 中实现 show.bs.modal 事件的一个示例: −
示例
<!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://ajax.googleapis.com/ajax/libs/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>Entrance Exams</h2> <p>The following is the result of the entrance exam:</p> <button type="button" class="btn btn-default btn-lg" id="button1">Result</button> <div class="modal fade" id="newModal" 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">Selected Students</h4> </div> <div class="modal-body"> <p>Harry and Chris cleared the entrance exam.</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(){ $("#newModal").modal("show"); }); $("#newModal").on('show.bs.modal', function () { alert('The modal will be displayed now!'); }); }); </script> </body> </html>
广告