Bootstrap 4 - 下拉菜单仅使用选项卡菜单工作一次

时间:2015-12-01 16:49:53

标签: toggle dropdown twitter-bootstrap-4 bootstrap-4

在bootstrap-4中,我第一次单击选项卡菜单中的下拉列表时工作正常,但之后它就会停止工作。

<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist">
  <li class="nav-item">
    <a class="nav-link active" href="#home" role="tab" data-toggle="tab">Home</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#profile" role="tab" data-toggle="tab">Profile</a>
  </li>
  <li class="nav-item dropdown">
    <a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">Dropdown</a>
    <div class="dropdown-menu">
      <a class="dropdown-item" href="#action" data-toggle="tab">action</a>
      <a class="dropdown-item" href="#action_2" data-toggle="tab">Another action</a>
    </div>
  </li>

</ul>

<!-- Tab panes -->
<div class="tab-content">
  <div role="tabpanel" class="tab-pane active" id="home">
    HOME
  </div>
  <div role="tabpanel" class="tab-pane" id="profile">
    PROFILE
  </div>
  <div role="tabpanel" class="tab-pane" id="action">
    ACTION 1
  </div>
  <div role="tabpanel" class="tab-pane" id="action_2">
    ACTION 2
  </div>
</div>

Here's a Demo in Bootply

1 个答案:

答案 0 :(得分:3)

问题似乎是由于在打开另一个标签时未从下拉列表中清除.active类,因此会将其突出显示并阻止再次点击同一下拉项目。

根据Issue #17371 - navs dropdown tab bug中的解决方法,您可以添加以下js,直到问题得到解决:

$('.nav-tabs').on('shown.bs.tab', 'a', function (e) {
    if (e.relatedTarget) {
        $(e.relatedTarget).removeClass('active');
    }
})

Stack Snippets中的演示

&#13;
&#13;
$('.nav-tabs').on('shown.bs.tab', 'a', function (e) {
    if (e.relatedTarget) {
        $(e.relatedTarget).removeClass('active');
    }
})
&#13;
<!-- 4.0.0-alpha.2 Libraries -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" >
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/js/bootstrap.min.js"></script>

<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist">
  <li class="nav-item">
    <a class="nav-link active" href="#home" role="tab" data-toggle="tab">Home</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#profile" role="tab" data-toggle="tab">Profile</a>
  </li>
  <li class="nav-item dropdown">
    <a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">Dropdown</a>
    <div class="dropdown-menu">
      <a class="dropdown-item" href="#action" data-toggle="tab">action</a>
      <a class="dropdown-item" href="#action_2" data-toggle="tab">Another action</a>
    </div>
  </li>

</ul>

<!-- Tab panes -->
<div class="tab-content">
  <div role="tabpanel" class="tab-pane active" id="home">
    HOME
  </div>
  <div role="tabpanel" class="tab-pane" id="profile">
    PROFILE
  </div>
  <div role="tabpanel" class="tab-pane" id="action">
    ACTION 1
  </div>
  <div role="tabpanel" class="tab-pane" id="action_2">
    ACTION 2
  </div>
</div>
&#13;
&#13;
&#13;