我有一个data-toggle="modal" data-target="#myModal"的div,它工作正常。
无论如何,当我在div中具有链接时,它们也将激活模式。有没有一种方法可以禁止a标签打开模式?



.team-section-inner{
  border: 1px solid;
}

 <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>

<div class="team-section-inner" data-toggle="modal" data-target="#myModal">
  <h1>activates modal</h1>
  <a href="#footer">Activates modal but needs to be disabled</a>
</div>

<!-- Modal -->
  <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">

      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Modal Header</h4>
        </div>
        <div class="modal-body">
          <p>Some text in the modal.</p>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>

    </div>
  </div>

最佳答案

您的活动正在冒泡。在div本身中单击a时,您需要取消该事件。不过,您需要jQuery。您可以使用stopPropagation()停止该操作。文件here

Here是什么是事件冒泡的很好解释。



$('.team-section-inner a').on('click', function(e){
  e.stopPropagation();
})

.team-section-inner{
  border: 1px solid;
}

<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>

<div class="team-section-inner" data-toggle="modal" data-target="#myModal">
  <h1>activates modal</h1>
  <a href="#footer">Activates modal but needs to be disabled</a>
</div>

<!-- Modal -->
  <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">

      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Modal Header</h4>
        </div>
        <div class="modal-body">
          <p>Some text in the modal.</p>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>

    </div>
  </div>

关于jquery - 数据切换禁用其中的a标签,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52703306/

10-09 14:49