Bootstrap Tabs (Tab) Plugin
Tabs (Tab) are Bootstrap Nav Elements introduced in the chapter. By combining some data attributes, you can easily create a tabbed interface. With this plugin you can place content in tabs, pill tabs, or even dropdown menu tabs.
If you want to reference the plugin's functionality separately, you need to include tab.js. Or, as Bootstrap Plugins Overview chapter mentions, you can include bootstrap.js or the compressed version bootstrap.min.js。
Usage
You can enable tabs in the following two ways:
- Via data attributes: you need to add data-toggle="tab" or data-toggle="pill" to the anchor text link.
add nav and nav-tabs class to the ul element, will apply the Bootstrap tab styles, add nav and nav-pills class to the ul element, will apply the Bootstrap Pill-style styles。
$('#myTab a').click(function (e) {
e.preventDefault()
$(this).tab('show')
})
The following examples demonstrate different ways to activate each tab:
// 通过名称选取标签页
$('#myTab a[href="#profile"]').tab('show')
// 选取第一个标签页
$('#myTab a:first').tab('show')
// 选取最后一个标签页
$('#myTab a:last').tab('show')
// 选取第三个标签页(从 0 开始索引)
$('#myTab li:eq(2) a').tab('show')
Fade effect
To set a fade effect for tabs, add .fade to each .tab-pane . The first tab must have the .in class added so the initial content fades in, as shown in the example below:
...
...
...
...
Example
The following example demonstrates the Tab plugin using data attributes and its fade effect:
Example
<ul id="myTab" class=">nav nav-tabs
"
<li class="active">
<a href="#home" data-toggle="tab">Runoob Tutorial
a
li
<li>
<a href="#ios" data-toggle="tab" iOS a li>
<li class="dropdown">
<a href="" id="myTabDrop1" class="dropdown-toggle" data-toggle="dropdown" Java>
<b class="caret" b a>
<ul class="dropdown-menu" role="menu" aria-labelledby="myTabDrop1">
<li>
<a href="#jmeter" tabindex="-1" data-toggle="tab" jmeter a li>
<li>
<a href="#ejb" tabindex="-1" data-toggle="tab" ejb a li ul li ul>
<div id="myTabContent" class="tab-content">
<div class=">tab-pane fade in active
"
id
=
"
home
"
<
p
Runoob Tutorial is a site offering the latest web technologies, providing free technical documentation on website building to help web technology enthusiasts get started quickly and build their own websites. "The early rookie flies and enters the trade sooner" - what you learn is not just technology, but also dreams.
p
div
<div class=">tab-pane fade
"
id
=
"
ios
"
<
p
iOS is a mobile operating system developed and released by Apple. It was first released in 2007 for the iPhone, iPod Touch, and Apple TV. iOS derives from OS X, and they share the Darwin foundation. The OS X operating system is used on Apple computers, while iOS is Apple's mobile version.
p
div
<div class=">tab-pane fade
"
id
=
"
jmeter
"
<
p
jMeter is an open-source testing software. It is a 100% pure Java application used for load and performance testing.
p
div
<div class=">tab-pane fade
"
id
=
"
ejb
"
<
p
Enterprise Java Beans (EJB) is a development architecture for creating highly scalable and powerful enterprise applications, deployed on J2EE-compatible application servers (such as JBOSS, Web Logic, etc.).
p
div
div
Try it »
The result is as follows:
Method
.$().tab
: This method activates a tab element and its content container. A tab needs to use a
data-target
or a pointing to a container node in the DOM
href
。
...
.....
Example
The following example demonstrates the Tab plugin method .tab . In this example, the second tab iOS is active:
Example
<ul id="myTab" class=">nav nav-tabs
"
<li class="active">
<a href="#home" data-toggle="tab">Runoob Tutorial
a
li
<li>
<a href="#ios" data-toggle="tab" iOS a li>
<li class="dropdown">
<a href="" id="myTabDrop1" class="dropdown-toggle" data-toggle="dropdown" Java>
<b class="caret" b a>
<ul class="dropdown-menu" role="menu" aria-labelledby="myTabDrop1">
<li>
<a href="#jmeter" tabindex="-1" data-toggle="tab" jmeter a li>
<li>
<a href="#ejb" tabindex="-1" data-toggle="tab" ejb a li ul li ul>
<div id="myTabContent" class="tab-content">
<div class=">tab-pane fade in active
"
id
=
"
home
"
<
p
Runoob Tutorial is a site offering the latest web technologies, providing free technical documentation on website building to help web technology enthusiasts get started quickly and build their own websites. "The early rookie flies and enters the trade sooner" - what you learn is not just technology, but also dreams.
p
div
<div class=">tab-pane fade
"
id
=
"
ios
"
<
p
iOS is a mobile operating system developed and released by Apple. It was first released in 2007 for the iPhone, iPod Touch, and Apple TV. iOS derives from OS X, and they share the Darwin foundation. The OS X operating system is used on Apple computers, while iOS is Apple's mobile version.
p
div
<div class=">tab-pane fade
"
id
=
"
jmeter
"
<
p
jMeter is an open-source testing software. It is a 100% pure Java application used for load and performance testing.
p
div
<div class=">tab-pane fade
"
id
=
"
ejb
"
<
p
Enterprise Java Beans (EJB) is a development architecture for creating highly scalable and powerful enterprise applications, deployed on J2EE-compatible application servers (such as JBOSS, Web Logic, etc.).
p
div
div
<
script
$(function () { $('#myTab li:eq(1) a').tab('show'); });
script
Try it »
The result is as follows:
Events
The table below lists the events used in the Tab plugin. These events can be used as hooks in functions.
| Events | Description | Example |
|---|---|---|
| show.bs.tab | This event fires when a tab is shown, but before the new tab has been displayed. Use event.target and event.relatedTarget to locate the active tab and the previously active tab, respectively. |
$('a[data-toggle="tab"]').on('show.bs.tab', function (e) { e.target // active tab e.relatedTarget // previously active tab })| onSelect | event | shown.bs.tab | This event fires when a tab is shown, but only after a tab has been displayed. Use event.target and event.relatedTarget to locate the active tab and the previously active tab. |
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
e.target // 激活的标签页
e.relatedTarget // 前一个激活的标签页
})
|
Example
The following example demonstrates the use of Tab plugin events. In this example, the current and previously visited tabs will be shown:
Example
<hr>
<p class="active-tab">
<strong>Active tab
strong
:
<span span p>
<p class="previous-tab">
<strong>Previously active tab
strong
:
<span span p>
<hr>
<ul id="myTab" class=">nav nav-tabs
"
<li class="active">
<a href="#home" data-toggle="tab">Runoob Tutorial
a
li
<li>
<a href="#ios" data-toggle="tab" iOS a li>
<li class="dropdown">
<a href="" id="myTabDrop1" class="dropdown-toggle" data-toggle="dropdown" Java>
<b class="caret" b a>
<ul class="dropdown-menu" role="menu" aria-labelledby="myTabDrop1">
<li>
<a href="#jmeter" tabindex="-1" data-toggle="tab" jmeter a li>
<li>
<a href="#ejb" tabindex="-1" data-toggle="tab" ejb a li ul li ul>
<div id="myTabContent" class="tab-content">
<div class=">tab-pane fade in active
"
id
=
"
home
"
<
p
Runoob Tutorial is a site offering the latest web technologies, providing free technical documentation on website building to help web technology enthusiasts get started quickly and build their own websites. "The early rookie flies and enters the trade sooner" - what you learn is not just technology, but also dreams.
p
div
<div class=">tab-pane fade
"
id
=
"
ios
"
<
p
iOS is a mobile operating system developed and released by Apple. It was first released in 2007 for the iPhone, iPod Touch, and Apple TV. iOS derives from OS X, and they share the Darwin foundation. The OS X operating system is used on Apple computers, while iOS is Apple's mobile version.
p
div
<div class=">tab-pane fade
"
id
=
"
jmeter
"
<
p
jMeter is an open-source testing software. It is a 100% pure Java application used for load and performance testing.
p
div
<div class=">tab-pane fade
"
id
=
"
ejb
"
<
p
Enterprise Java Beans (EJB) is a development architecture for creating highly scalable and powerful enterprise applications, deployed on J2EE-compatible application servers (such as JBOSS, Web Logic, etc.).
p
div
div
<
script
$(function(){ $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) { // get the name of the active tab var activeTab = $(e.target).text(); // get the name of the previously active tab var previousTab = $(e.relatedTarget).text(); $(".active-tab span").html(activeTab); $(".previous-tab span").html(previousTab); }); });
script
Try it »
The result is as follows:
If you want to reference the plugin's functionality separately, you need to include tab.js. Or, as