當模態視窗即將顯示時的載入程式事件


當模態視窗即將顯示時,Bootstrap 中的 show.bs.modal 事件被觸發。在此,我添加了一個生成警報的指令碼 −

<script>
$(document).ready(function(){
  $("#button1").click(function(){
    $("#newModal").modal("show");
  });
  $("#newModal").on('show.bs.modal', function () {
    alert('The modal will be displayed now!');
  });
});
</script>

在點選按鈕時,上述示例程式碼會生成模態視窗。之後,我使用了 show.bs.modal 事件,以便在模態視窗即將變得可見時生成警報。

以下示例演示了當模態視窗即將顯示時,如何在 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>Team</h2>
  <p>The following is the result of the selections:</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 Players</h4>
        </div>
        <div class="modal-body">
          <p>Tom and Jack selected in the team.</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>

更新時間: 15-6 月-2020

188 次瀏覽

開啟您的職業生涯

完成課程即可獲得認證

立即開始
廣告
© . All rights reserved.