防止塌陷在模式开启时崩溃

时间:2019-04-12 02:54:13

标签: javascript jquery html twitter-bootstrap modal-dialog

我试图在不触发崩溃点击事件的情况下打开和关闭模态,但是由于模态按钮是崩溃点击元素的子代,因此我找不到解决方法。

e.stopPropagation()可以工作,但是会引起其他问题,因此不是一种选择。

JSFiddle

.wrap {
  background: green;
  display: flex;
  justify-content: center;
}

.well {
  background: red !important;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.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>
<div class="wrap" data-toggle="collapse" href="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
  <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  Modal
</button>
</div>
<div class="collapse" id="collapseExample">
  <div class="well">
  </div>
</div>

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:2)

您可以在jquery下添加,以防止崩溃崩溃

$('.modalclk').click(function(){
  $('.wrap').attr('data-toggle','');
});
$(".close, button[data-dismiss='modal']").click(function(){
  $('.wrap').attr('data-toggle','collapse');
});
$('body').on("click", function(e) {
  if ($(e.target).hasClass('modal')) {
    $('.wrap').attr('data-toggle','collapse');
  }
});
.wrap {
  background: green;
  display: flex;
  justify-content: center;
}

.well {
  background: red !important;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.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>
<div class="wrap" data-toggle="collapse" href="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
  <button type="button" class="btn btn-primary btn-lg modalclk" data-toggle="modal" data-target="#myModal">
  Modal
</button>
</div>
<div class="collapse" id="collapseExample">
  <div class="well">
  </div>
</div>

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>