【问题标题】:How can I select a tab when changing a selected option?更改所选选项时如何选择选项卡?
【发布时间】:2019-10-09 20:59:59
【问题描述】:

如何在 Bootstrap 4 中选择下拉选项时切换选项卡?

<ul class="nav nav-tabs" id="myTab" role="tablist">
  <li class="nav-item">
    <a class="nav-link active" id="home-tab" data-toggle="tab" href="#home" role="tab" aria-controls="home" aria-selected="true">Home</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" id="profile-tab" data-toggle="tab" href="#profile" role="tab" aria-controls="profile" aria-selected="false">Profile</a>
  </li>
</ul>
<div class="tab-content" id="myTabContent">
  <div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">...</div>
  <div class="tab-pane fade" id="profile" role="tabpanel" aria-labelledby="profile-tab">...</div>
</div>

【问题讨论】:

标签: jquery twitter-bootstrap bootstrap-4


【解决方案1】:

根据我对您的问题的理解做了一些事情,尽管它非常粗略地描述了您的需求。看看这是否有用。

HTML:

<select class="nav-select">
      <option href="#home" selected>Home</option>
      <option href="#profile">Profile</option>
    </select>
    <div class="tab-content" id="myTabContent">
      <div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">...</div>
      <div class="tab-pane fade" id="profile" role="tabpanel" aria-labelledby="profile-tab">Profile</div>
    </div>

JS:

$('select.nav-select').on('change',function(){
   var selected_id=$(this).find('option:selected').attr('href');
  $('.tab-content .tab-pane').removeClass('show active');
  $(selected_id).addClass('show active');
});

【讨论】:

  • 这是正确的想法,但Tab has methods 做你正在做的手动操作。最好使用它们以保持同步。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-05
相关资源
最近更新 更多