数据切换禁用其中的a标签

时间:2018-10-08 13:22:19

标签: jquery html css twitter-bootstrap bootstrap-modal

我有一个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>

2 个答案:

答案 0 :(得分:1)

您的活动正在冒泡。在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>

答案 1 :(得分:0)

不,您不能;只需在触发模态的div之前添加h1即可解决问题

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