3

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

这是 Bootply 中的演示

4

1 回答 1

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');
    }
})

堆栈片段中的演示

$('.nav-tabs').on('shown.bs.tab', 'a', function (e) {
    if (e.relatedTarget) {
        $(e.relatedTarget).removeClass('active');
    }
})
<!-- 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>

于 2016-03-26T15:02:10.923 回答