當模態框將要被隱藏時觸發的 Bootstrap 事件


Bootstrap中的hide.bs.modal事件在模態框將要被隱藏時觸發。

首先,單擊按鈕時隱藏 Bootstrap 模態框 -

$("#button1").click(function(){
  $("#newModal").modal("hide");
});

現在,使用 hide.bs.modal 類,並在單擊按鈕時生成彈出提示,以在模態框將要隱藏時彈出提示 -

$("#newModal").on('hide.bs.modal', function () {
  alert('The modal is about to be hidden.');
});

讓我們看一個說明 hide.bs.modal 事件用法的示例 -

示例

即時演示

<!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="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>
    <style>
    #button1 {
      width: 140px;
      padding: 20px;
      bottom: 150px;
      z-index: 9999;
      font-size:15px;
      position: absolute;
      margin: 0 auto;
    }
    </style>
  </head>
<body>
  <div class="container">
    <h2>Sample</h2>
    <p>This is demo text.</p>
    <p>This is demo text.</p>
    <p>This is demo text.</p>
    <p>This is demo text.</p>
    <p>This is demo text.</p>
    <p>This is demo text.</p>
    <p>This is demo text.</p>
    <p>This is demo text.</p>
    <p>This is demo text.</p>
    <p>This is demo text.</p>
    <p>This is demo text.</p>
    <button type="button" class="btn btn-default btn-lg" id="button1">Click to hide</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">Sample Modal</h4>
          </div>
        <div class="modal-body">
          <p>This is demo text.</p>
        </div>
      </div>
    </div>
 </div>
</div>

<script>
  $(document).ready(function(){
    $("#newModal").modal("show");
    $("#button1").click(function(){
      $("#newModal").modal("hide");
    });
    $("#newModal").on('hide.bs.modal', function () {
      alert('The modal is about to be hidden.');
    });
  });
</script>
</body>
</html>

更新時間: 18-6 月 -2020

69 次瀏覽

啟動你的職業生涯

完成該課程獲得認證

開始吧
廣告
© . All rights reserved.