标签页

使用此 JavaScript 插件,通过我们的任何导航或列表组组件创建可切换的内容面板。

Requires JS
1 dependency

标签页插件可与导航组件(如 .nav-tabs 和 .nav-pills)以及列表组配合使用。

导航标签页

为导航添加所需的数据属性和 id,使其可切换。然后,添加带有匹配 id 的 tab-content 容器到每个标签页面板。

This is some placeholder content the Home tab’s associated content.

This is some placeholder content the Profile tab’s associated content.

This is some placeholder content the Contact tab’s associated content.

This is some placeholder content the Disabled tab’s associated content.

HTML
<ul class="nav nav-tabs" id="myTab" role="tablist">
  <li class="nav-item" role="presentation">
    <button class="nav-link active" id="home-tab" data-bs-toggle="tab" data-bs-target="#home-tab-pane" type="button" role="tab" aria-controls="home-tab-pane" aria-selected="true">Home</button>
  </li>
  <li class="nav-item" role="presentation">
    <button class="nav-link" id="profile-tab" data-bs-toggle="tab" data-bs-target="#profile-tab-pane" type="button" role="tab" aria-controls="profile-tab-pane" aria-selected="false">Profile</button>
  </li>
  <li class="nav-item" role="presentation">
    <button class="nav-link" id="contact-tab" data-bs-toggle="tab" data-bs-target="#contact-tab-pane" type="button" role="tab" aria-controls="contact-tab-pane" aria-selected="false">Contact</button>
  </li>
  <li class="nav-item" role="presentation">
    <button class="nav-link" id="disabled-tab" data-bs-toggle="tab" data-bs-target="#disabled-tab-pane" type="button" role="tab" aria-controls="disabled-tab-pane" aria-selected="false" disabled>Disabled</button>
  </li>
</ul>
<div class="tab-content" id="myTabContent">
  <div class="tab-pane show active" id="home-tab-pane" role="tabpanel" aria-labelledby="home-tab" tabindex="0">
    <p>This is some placeholder content the <strong>Home tab’s</strong> associated content.</p>
  </div>
  <div class="tab-pane" id="profile-tab-pane" role="tabpanel" aria-labelledby="profile-tab" tabindex="0">
    <p>This is some placeholder content the <strong>Profile tab’s</strong> associated content.</p>
  </div>
  <div class="tab-pane" id="contact-tab-pane" role="tabpanel" aria-labelledby="contact-tab" tabindex="0">
    <p>This is some placeholder content the <strong>Contact tab’s</strong> associated content.</p>
  </div>
  <div class="tab-pane" id="disabled-tab-pane" role="tabpanel" aria-labelledby="disabled-tab" tabindex="0">
    <p>This is some placeholder content the <strong>Disabled tab’s</strong> associated content.</p>
  </div>
</div>

使用按钮

为了满足你的需求,此功能可与上面展示的基于 <ul> 的标记配合使用,也可与任何自定义的“自行构建”标记配合使用。请注意,如果你使用 <nav>,不应直接为其添加 role="tablist",因为这会覆盖该元素作为导航地标的原生角色。相反,请改用其他元素(下面的示例中使用了一个简单的 <div>),并将 <nav> 包裹在其外层。

HTML
<nav>
  <div class="nav nav-tabs" id="nav-tab2" role="tablist">
    <button class="nav-link active" id="nav-home-tab2" data-bs-toggle="tab" data-bs-target="#nav-home2" type="button" role="tab" aria-controls="nav-home2" aria-selected="true">Home</button>
    <button class="nav-link" id="nav-profile-tab2" data-bs-toggle="tab" data-bs-target="#nav-profile2" type="button" role="tab" aria-controls="nav-profile2" aria-selected="false">Profile</button>
    <button class="nav-link" id="nav-contact-tab2" data-bs-toggle="tab" data-bs-target="#nav-contact2" type="button" role="tab" aria-controls="nav-contact2" aria-selected="false">Contact</button>
    <button class="nav-link" id="nav-disabled-tab2" data-bs-toggle="tab" data-bs-target="#nav-disabled2" type="button" role="tab" aria-controls="nav-disabled2" aria-selected="false" disabled>Disabled</button>
  </div>
</nav>
<div class="tab-content" id="nav-tabContent2">
  <div class="tab-pane show active" id="nav-home2" role="tabpanel" aria-labelledby="nav-home-tab2" tabindex="0">
    <p>This is some placeholder content the <strong>Home tab’s</strong> associated content.</p>
  </div>
  <div class="tab-pane" id="nav-profile2" role="tabpanel" aria-labelledby="nav-profile-tab2" tabindex="0">
    <p>This is some placeholder content the <strong>Profile tab’s</strong> associated content.</p>
  </div>
  <div class="tab-pane" id="nav-contact2" role="tabpanel" aria-labelledby="nav-contact-tab2" tabindex="0">
    <p>This is some placeholder content the <strong>Contact tab’s</strong> associated content.</p>
  </div>
  <div class="tab-pane" id="nav-disabled2" role="tabpanel" aria-labelledby="nav-disabled-tab2" tabindex="0">
    <p>This is some placeholder content the <strong>Disabled tab’s</strong> associated content.</p>
  </div>
</div>

胶囊导航

标签页插件也可与胶囊导航配合使用。

This is some placeholder content the Home tab’s associated content.

This is some placeholder content the Profile tab’s associated content.

This is some placeholder content the Contact tab’s associated content.

This is some placeholder content the Disabled tab’s associated content.

HTML
<ul class="nav nav-pills" id="pills-tab" role="tablist">
  <li class="nav-item" role="presentation">
    <button class="nav-link active" id="pills-home-tab" data-bs-toggle="tab" data-bs-target="#pills-home" type="button" role="tab" aria-controls="pills-home" aria-selected="true">Home</button>
  </li>
  <li class="nav-item" role="presentation">
    <button class="nav-link" id="pills-profile-tab" data-bs-toggle="tab" data-bs-target="#pills-profile" type="button" role="tab" aria-controls="pills-profile" aria-selected="false">Profile</button>
  </li>
  <li class="nav-item" role="presentation">
    <button class="nav-link" id="pills-contact-tab" data-bs-toggle="tab" data-bs-target="#pills-contact" type="button" role="tab" aria-controls="pills-contact" aria-selected="false">Contact</button>
  </li>
  <li class="nav-item" role="presentation">
    <button class="nav-link" id="pills-disabled-tab" data-bs-toggle="tab" data-bs-target="#pills-disabled" type="button" role="tab" aria-controls="pills-disabled" aria-selected="false" disabled>Disabled</button>
  </li>
</ul>
<div class="tab-content" id="pills-tabContent">
  <div class="tab-pane show active" id="pills-home" role="tabpanel" aria-labelledby="pills-home-tab" tabindex="0">
    <p>This is some placeholder content the <strong>Home tab’s</strong> associated content.</p>
  </div>
  <div class="tab-pane" id="pills-profile" role="tabpanel" aria-labelledby="pills-profile-tab" tabindex="0">
    <p>This is some placeholder content the <strong>Profile tab’s</strong> associated content.</p>
  </div>
  <div class="tab-pane" id="pills-contact" role="tabpanel" aria-labelledby="pills-contact-tab" tabindex="0">
    <p>This is some placeholder content the <strong>Contact tab’s</strong> associated content.</p>
  </div>
  <div class="tab-pane" id="pills-disabled" role="tabpanel" aria-labelledby="pills-disabled-tab" tabindex="0">
    <p>This is some placeholder content the <strong>Disabled tab’s</strong> associated content.</p>
  </div>
</div>

以及垂直胶囊导航。理想情况下,对于垂直标签页,你还应为标签页列表容器添加 aria-orientation="vertical"。

This is some placeholder content the Home tab’s associated content.

This is some placeholder content the Profile tab’s associated content.

This is some placeholder content the Disabled tab’s associated content.

This is some placeholder content the Messages tab’s associated content.

This is some placeholder content the Settings tab’s associated content.

HTML
<div class="d-flex align-items-start">
<div class="nav nav-pills nav-pills-vertical me-4" id="v-pills-tab" role="tablist" aria-orientation="vertical">
  <button class="nav-link active" id="v-pills-home-tab" data-bs-toggle="tab" data-bs-target="#v-pills-home" type="button" role="tab" aria-controls="v-pills-home" aria-selected="true">Home</button>
  <button class="nav-link" id="v-pills-profile-tab" data-bs-toggle="tab" data-bs-target="#v-pills-profile" type="button" role="tab" aria-controls="v-pills-profile" aria-selected="false">Profile</button>
  <button class="nav-link" id="v-pills-disabled-tab" data-bs-toggle="tab" data-bs-target="#v-pills-disabled" type="button" role="tab" aria-controls="v-pills-disabled" aria-selected="false" disabled>Disabled</button>
  <button class="nav-link" id="v-pills-messages-tab" data-bs-toggle="tab" data-bs-target="#v-pills-messages" type="button" role="tab" aria-controls="v-pills-messages" aria-selected="false">Messages</button>
  <button class="nav-link" id="v-pills-settings-tab" data-bs-toggle="tab" data-bs-target="#v-pills-settings" type="button" role="tab" aria-controls="v-pills-settings" aria-selected="false">Settings</button>
</div>
<div class="tab-content" id="v-pills-tabContent">
  <div class="tab-pane show active" id="v-pills-home" role="tabpanel" aria-labelledby="v-pills-home-tab" tabindex="0">
    <p>This is some placeholder content the <strong>Home tab’s</strong> associated content.</p>
  </div>
  <div class="tab-pane" id="v-pills-profile" role="tabpanel" aria-labelledby="v-pills-profile-tab" tabindex="0">
    <p>This is some placeholder content the <strong>Profile tab’s</strong> associated content.</p>
  </div>
  <div class="tab-pane" id="v-pills-disabled" role="tabpanel" aria-labelledby="v-pills-disabled-tab" tabindex="0">
    <p>This is some placeholder content the <strong>Disabled tab’s</strong> associated content.</p>
  </div>
  <div class="tab-pane" id="v-pills-messages" role="tabpanel" aria-labelledby="v-pills-messages-tab" tabindex="0">
    <p>This is some placeholder content the <strong>Messages tab’s</strong> associated content.</p>
  </div>
  <div class="tab-pane" id="v-pills-settings" role="tabpanel" aria-labelledby="v-pills-settings-tab" tabindex="0">
    <p>This is some placeholder content the <strong>Settings tab’s</strong> associated content.</p>
  </div>
</div>
</div>

列表组

标签页插件也可与列表组配合使用。为每个 .list-group-item 添加 data-bs-toggle="tab",并将它们与 .tab-pane 元素配对。

Some placeholder content in a paragraph relating to "Home".

Some placeholder content in a paragraph relating to "Profile".

Some placeholder content in a paragraph relating to "Messages".

Some placeholder content in a paragraph relating to "Settings".

HTML
<div class="row">
<div class="col-4">
  <div class="list-group" id="list-tab" role="tablist">
    <a class="list-group-item list-group-item-action active" id="list-home-list" data-bs-toggle="tab" href="#list-home" role="tab" aria-controls="list-home">Home</a>
    <a class="list-group-item list-group-item-action" id="list-profile-list" data-bs-toggle="tab" href="#list-profile" role="tab" aria-controls="list-profile">Profile</a>
    <a class="list-group-item list-group-item-action" id="list-messages-list" data-bs-toggle="tab" href="#list-messages" role="tab" aria-controls="list-messages">Messages</a>
    <a class="list-group-item list-group-item-action" id="list-settings-list" data-bs-toggle="tab" href="#list-settings" role="tab" aria-controls="list-settings">Settings</a>
  </div>
</div>
<div class="col-8">
  <div class="tab-content" id="list-tabContent">
    <div class="tab-pane show active" id="list-home" role="tabpanel" aria-labelledby="list-home-list">
      <p>Some placeholder content in a paragraph relating to "Home".</p>
    </div>
    <div class="tab-pane" id="list-profile" role="tabpanel" aria-labelledby="list-profile-list">
      <p>Some placeholder content in a paragraph relating to "Profile".</p>
    </div>
    <div class="tab-pane" id="list-messages" role="tabpanel" aria-labelledby="list-messages-list">
      <p>Some placeholder content in a paragraph relating to "Messages".</p>
    </div>
    <div class="tab-pane" id="list-settings" role="tabpanel" aria-labelledby="list-settings-list">
      <p>Some placeholder content in a paragraph relating to "Settings".</p>
    </div>
  </div>
</div>
</div>

哈希链接

当 Bootstrap 在页面之后加载时,哈希链接也能正常工作。后续的片段更改会取消之前目标的待处理滚动。

为标签页面板添加 data-bs-hash,以便在 URL 片段与其 id 匹配时显示它。使用普通的页内链接来试用。如果使用吸顶或固定导航栏,你可能需要使用 scroll-margin-block-start 进行一些视口偏移。

#hash-home #hash-profile #hash-contact

This is some placeholder content the Home tab’s associated content.

This is some placeholder content the Profile tab’s associated content.

This is some placeholder content the Contact tab’s associated content.

HTML
<p class="d-inline-flex gap-2 mb-0">
  <a href="#hash-home">#hash-home</a>
  <a href="#hash-profile">#hash-profile</a>
  <a href="#hash-contact">#hash-contact</a>
</p>
<ul class="nav nav-tabs" id="hashTab" role="tablist">
  <li class="nav-item" role="presentation">
    <button class="nav-link active" id="hash-home-tab" data-bs-toggle="tab" data-bs-target="#hash-home" type="button" role="tab" aria-controls="hash-home" aria-selected="true">Home</button>
  </li>
  <li class="nav-item" role="presentation">
    <button class="nav-link" id="hash-profile-tab" data-bs-toggle="tab" data-bs-target="#hash-profile" type="button" role="tab" aria-controls="hash-profile" aria-selected="false">Profile</button>
  </li>
  <li class="nav-item" role="presentation">
    <button class="nav-link" id="hash-contact-tab" data-bs-toggle="tab" data-bs-target="#hash-contact" type="button" role="tab" aria-controls="hash-contact" aria-selected="false">Contact</button>
  </li>
</ul>
<div class="tab-content" id="hashTabContent">
  <div class="tab-pane show active" id="hash-home" role="tabpanel" aria-labelledby="hash-home-tab" tabindex="0" data-bs-hash>
    <p>This is some placeholder content the <strong>Home tab’s</strong> associated content.</p>
  </div>
  <div class="tab-pane" id="hash-profile" role="tabpanel" aria-labelledby="hash-profile-tab" tabindex="0" data-bs-hash>
    <p>This is some placeholder content the <strong>Profile tab’s</strong> associated content.</p>
  </div>
  <div class="tab-pane" id="hash-contact" role="tabpanel" aria-labelledby="hash-contact-tab" tabindex="0" data-bs-hash>
    <p>This is some placeholder content the <strong>Contact tab’s</strong> associated content.</p>
  </div>
</div>

可访问性

如 ARIA 创作实践指南标签页面板模式中所述,动态标签页界面需要 role="tablist"、role="tab"、role="tabpanel" 以及其他 aria- 属性,以便向辅助技术(如屏幕阅读器)用户传达其结构、功能和当前状态。作为最佳实践,我们建议为标签页使用 <button> 元素,因为它们是触发动态更改的控件,而不是导航到新页面或位置的链接。

与 ARIA 创作实践模式一致,只有当前激活的标签页接收键盘焦点。当 JavaScript 插件初始化时,它会在所有非激活的标签页控件上设置 tabindex="-1"。一旦当前激活的标签页获得焦点,光标键会激活上一个/下一个标签页。Home 和 End 键分别激活第一个和最后一个标签页。插件将相应地更改游走式 tabindex。但请注意,在光标键交互方面,JavaScript 插件不区分水平和垂直标签页列表:无论标签页列表的方向如何,向上和向左光标都会转到上一个标签页,而向下和向右光标都会转到下一个标签页。

通常,为便于键盘导航,建议让标签页面板本身也可聚焦,除非标签页面板内包含有意义内容的第一个元素已经可聚焦。JavaScript 插件不会尝试处理这方面——在适当的情况下,你需要通过在标记中添加 tabindex="0" 来显式使标签页面板可聚焦。

标签页 JavaScript 插件不支持包含菜单的标签页界面,因为这会导致可用性和可访问性问题。从可用性角度来看,当前显示的标签页触发元素不立即可见(因为它在关闭的菜单内),这会造成混淆。从可访问性角度来看,目前没有合理的方式将这种构造映射到标准的 WAI ARIA 模式,这意味着无法轻易让辅助技术用户理解。

用法

通过 data 属性

你无需编写任何 JavaScript,只需在元素上指定 data-bs-toggle="tab" 即可激活标签页导航。

属性描述
data-bs-toggle="tab"在触发器上激活标签页导航。
data-bs-target要显示的标签页面板的 CSS 选择器(必须与相应的 .tab-pane 配对)。
data-bs-hash在标签页面板上设置时,当 URL 片段与其 id 匹配时显示该面板(在加载和 hashchange 时)。请参阅哈希链接。
HTML
          <!-- Nav tabs -->
<ul class="nav nav-tabs" id="myTab" role="tablist">
  <li class="nav-item" role="presentation">
    <button class="nav-link active" id="home-tab" data-bs-toggle="tab" data-bs-target="#home" type="button" role="tab" aria-controls="home" aria-selected="true">Home</button>
  </li>
  <li class="nav-item" role="presentation">
    <button class="nav-link" id="profile-tab" data-bs-toggle="tab" data-bs-target="#profile" type="button" role="tab" aria-controls="profile" aria-selected="false">Profile</button>
  </li>
  <li class="nav-item" role="presentation">
    <button class="nav-link" id="messages-tab" data-bs-toggle="tab" data-bs-target="#messages" type="button" role="tab" aria-controls="messages" aria-selected="false">Messages</button>
  </li>
  <li class="nav-item" role="presentation">
    <button class="nav-link" id="settings-tab" data-bs-toggle="tab" data-bs-target="#settings" type="button" role="tab" aria-controls="settings" aria-selected="false">Settings</button>
  </li>
</ul>

<!-- Tab panes -->
<div class="tab-content">
  <div class="tab-pane active" id="home" role="tabpanel" aria-labelledby="home-tab" tabindex="0">...</div>
  <div class="tab-pane" id="profile" role="tabpanel" aria-labelledby="profile-tab" tabindex="0">...</div>
  <div class="tab-pane" id="messages" role="tabpanel" aria-labelledby="messages-tab" tabindex="0">...</div>
  <div class="tab-pane" id="settings" role="tabpanel" aria-labelledby="settings-tab" tabindex="0">...</div>
</div>
        

通过 JavaScript

通过 JavaScript 启用可切换的标签页(每个标签页需要单独激活):

JavaScript
          const triggerTabList = document.querySelectorAll('#myTab button')
triggerTabList.forEach(triggerEl => {
  const tabTrigger = new bootstrap.Tab(triggerEl)

  triggerEl.addEventListener('click', event => {
    event.preventDefault()
    tabTrigger.show()
  })
})
        

你可以通过多种方式激活各个标签页:

JavaScript
          const triggerEl = document.querySelector('#myTab button[data-bs-target="#profile"]')
bootstrap.Tab.getInstance(triggerEl).show() // Select tab by name

const triggerFirstTabEl = document.querySelector('#myTab li:first-child button')
bootstrap.Tab.getInstance(triggerFirstTabEl).show() // Select first tab
        

淡入效果

每个 .tab-pane 在变为激活状态时淡入。你选择的页面板会立即离开,因为两个在流中的面板无法在没有一个堆叠在另一个下面的情况下交叉淡入淡出。

为面板添加 .tab-pane-instant 以关闭其淡入,或使用 --bs-tab-pane-transition-* tokens 重新调整它。请参阅过渡。

HTML
          <div class="tab-content">
  <div class="tab-pane tab-pane-instant show active" id="home" role="tabpanel" aria-labelledby="home-tab" tabindex="0">...</div>
  <div class="tab-pane tab-pane-instant" id="profile" role="tabpanel" aria-labelledby="profile-tab" tabindex="0">...</div>
</div>
        

依赖

The tab plugin requires the following JavaScript files if you’re building Bootstrap’s JS from source:

FileDescription
js/src/tab.tsMain tab component
js/src/base-component.tsBase component class
js/src/dom/data.tsElement data store
js/src/dom/event-handler.tsEvent handling utilities
js/src/dom/manipulator.tsData attribute manipulation
js/src/dom/selector-engine.tsDOM selector utilities
js/src/util/config.tsConfiguration base class
js/src/util/hash-target.tsURL hash target helpers
js/src/util/index.tsCore utility functions

方法

用于显示或隐藏组件的方法会返回一个 promise。 该 promise 在过渡结束时 resolve,因此 await 会在与 shown.bs.* 或 hidden.bs.* 监听器触发的同一时刻继续执行。这些方法仍会立即返回,绝不会阻塞调用方。对正处于过渡中的组件进行调用不会产生任何效果,并会立即 resolve。在我们的 JavaScript 文档中了解更多。

将你的内容激活为标签页元素。

你可以使用构造函数创建标签页实例,例如:

JavaScript
          const bsTab = new bootstrap.Tab('#myTab')
        
方法描述
dispose销毁元素的标签页。
getInstance静态方法,允许你获取与 DOM 元素关联的标签页实例,可以这样使用:bootstrap.Tab.getInstance(element)。
getOrCreateInstance静态方法,返回与 DOM 元素关联的标签页实例,如果尚未初始化则创建一个新实例。可以这样使用:bootstrap.Tab.getOrCreateInstance(element)。
show选择给定的标签页并显示其关联的面板。之前选择的任何其他标签页将变为未选中状态,其关联的面板将被隐藏。返回一个 promise,在标签页面板显示后解析(即在 shown.bs.tab 事件触发后)。

事件

显示新标签页时,事件按以下顺序触发:

  1. hide.bs.tab(在当前激活的标签页上)
  2. show.bs.tab(在即将显示的标签页上)
  3. hidden.bs.tab(在之前激活的标签页上,与 hide.bs.tab 事件相同)
  4. shown.bs.tab(在新激活的刚显示的标签页上,与 show.bs.tab 事件相同)

如果没有已激活的标签页,则不会触发 hide.bs.tab 和 hidden.bs.tab 事件。

事件类型描述
hide.bs.tab当即将显示新标签页(因此之前的激活标签页即将隐藏)时触发此事件。使用 event.target 和 event.relatedTarget 分别定位当前激活的标签页和即将激活的新标签页。
hidden.bs.tab在新标签页显示后(因此之前的激活标签页已隐藏)触发此事件。使用 event.target 和 event.relatedTarget 分别定位之前的激活标签页和新的激活标签页。
show.bs.tab在新标签页显示之前、标签页显示时触发此事件。使用 event.target 和 event.relatedTarget 分别定位激活的标签页和之前的激活标签页(如果可用)。
shown.bs.tab在标签页显示后、标签页显示时触发此事件。使用 event.target 和 event.relatedTarget 分别定位激活的标签页和之前的激活标签页(如果可用)。
JavaScript
          const tabEl = document.querySelector('button[data-bs-toggle="tab"]')
tabEl.addEventListener('shown.bs.tab', event => {
  event.target // newly activated tab
  event.relatedTarget // previous active tab
})