.modal('dispose')有什么作用? Bootstrap 4

时间:2018-05-31 14:14:58

标签: jquery html twitter-bootstrap bootstrap-4

根据文件.modal('dispose'),破坏了模态。

  

.modal( '处置')

     

销毁元素的模态。

但是当我使用

创建eventListener
$('#myModal').on('hidden.bs.modal', function (event) {
    // Destroy modal
    $('#myModal').modal('dispose');
});

模态仍然是文档的一部分。文档让我困惑,这个方法不用于从文档中删除模态吗?这种方法的目的是什么?

$('#myModal').modal('show');

$('#myModal').on('hidden.bs.modal', function (event) {
  console.log('Destroy modal');
  $('#myModal').modal('dispose');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.bundle.min.js" integrity="sha384-u/bQvRA/1bobcXlcEYpsEdFVK/vJs3+T+nXLsBYJthmdBuavHvAW6UsmqO2Gd/F9" crossorigin="anonymous"></script>
<div class="modal" tabindex="-1" role="dialog" id="myModal">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <p>Modal body text goes here.</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary">Save changes</button>
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:6)

它破坏了Bootstrap的Modal组件的jQuery实例。它不会从DOM中删除Modal标记。