当模态框被完全隐藏时触发 Bootstrap 事件


Bootstrap 中的 hidden.bs.modal 事件在模态框完全隐藏时触发。

首先,单击隐藏按钮以隐藏模态框 −

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

单击按钮后,hidden.bs.modal 触发并使用以下脚本生成一个警报 −

$("#newModal").on('hidden.bs.modal', function () {
  alert('The modal is completely hidden now!');
});

让我们看一个实现 hidden.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>

  <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">Information</h4>
          </div>
          <div class="modal-body">
            <p>Crucial Information about the Examination.</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!');
    });
    $("#newModal").on('hidden.bs.modal', function () {
      alert('The modal is completely hidden now!');
    });
});
</script>

</body>
</html>

更新日期: 15-6-2020

605 次浏览

开启你的 职业生涯

完成课程即可获得认证

开始学习
广告