Bootstrap Tab Plugin
Tabs (Tab) inBootstrap Navigation ElementsWe introduced this 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-style tabs, or even dropdown menu tabs.
If you want to reference the functionality of this plugin separately, you need to referencetab.js. Or, asBootstrap Plugins Overviewmentioned in the chapter, you can referencebootstrap.jsor the compressed version ofbootstrap.min.js。
Usage
You can enable tabs in the following two ways:
- Via the data attribute: you need to adddata-toggle="tab"ordata-toggle="pill"to the anchor text link.
Addnavandnav-tabsclass toulin, will apply Bootstraptab styles, addnavandnav-pillsclass toulin, will apply Bootstrappill styles。
<ul class="nav nav-tabs">
<li><a href="../index.html" data-toggle="tab">Home</a></li>
...
</ul>
- Via JavaScript: you can use JavaScript to enable tabs, as shown below:
$('#myTab a').click(function (e) {
e.preventDefault()
$(this).tab('show')
})
The following example demonstrates activating each tab in different ways:
// 通过名称选取标签页
$('#myTab a[href="../index.html"]').tab('show')
// 选取第一个标签页
$('#myTab a:first').tab('show')
// 选取最后一个标签页
$('#myTab a:last').tab('show')
// 选取第三个标签页(从 0 开始索引)
$('#myTab li:eq(2) a').tab('show')
Fade effect
If you need to add a fade effect to tabs, please add.fadeto each.tab-paneafter. The first tab must add.inclass so that the initial content fades in, as shown in the example below:
<div class="tab-content">
<div class="tab-pane fade in active" id="home">...</div>
<div class="tab-pane fade" id="svn">...</div>
<div class="tab-pane fade" id="ios">...</div>
<div class="tab-pane fade" id="java">...</div>
</div>
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">Example</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>Example is a website providing the latest web technologies. This site offers free technical documentation related to website building, helping web technology enthusiasts quickly get started and create their own websites. Rookies fly early — what we learn is not only technology, but dreams.</p>
</div>
<div class="tab-pane fade" id="ios">
<p>iOS is a mobile operating system developed and released by Apple Inc. It was first released in 2007 for iPhone, iPod Touch, and Apple TV. iOS is derived from OS X, and they share the Darwin foundation. OS X is the operating system used on Apple computers, and 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-level applications, deployed on J2EE compatible application servers (such as JBOSS, Web Logic, etc.).</p>
</div>
</div>
Try it Yourself »
The result is shown below:
Methods
.$().tab: This method can activate tab elements and content containers. Tabs need to use a
data-targetor a pointer to a container node in the DOM
href。
<ul class="nav nav-tabs" id="myTab">
<li class="active"><a href="../index.html" data-toggle="tab">Home</a></li>
.....
</ul>
<div class="tab-content">
<div class="tab-pane active" id="home">...</div>
.....
</div>
<script>
$(function () {
$('#myTab a:last').tab('show')
})
</script>
Example
The following example demonstrates the Tab plugin's methods.tabusage. In this example, the second tabiOSis activated:
Example
<ul id="myTab" class="nav nav-tabs">
<li class="active"><a href="#home" data-toggle="tab">Example</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>Example is a website providing the latest web technologies. This site offers free technical documentation related to website building, helping web technology enthusiasts quickly get started and create their own websites. Rookies fly early — what we learn is not only technology, but dreams.</p>
</div>
<div class="tab-pane fade" id="ios">
<p>iOS is a mobile operating system developed and released by Apple Inc. It was first released in 2007 for iPhone, iPod Touch, and Apple TV. iOS is derived from OS X, and they share the Darwin foundation. OS X is the operating system used on Apple computers, and 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 robust enterprise-level 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 following table lists the events used in the Tab plugin. These events can be used as hooks in functions.
| Event | Description | Example |
| show.bs.tab | This event fires when the tab is shown, but before the new tab has been shown. Use respectivelyevent.targetandevent.relatedTargetto target the active tab and the previously active tab. |
$('a[data-toggle="tab"]').on('show.bs.tab', function (e) {
e.target // 激活的标签页
e.relatedTarget // 前一个激活的标签页
})
|
| shown.bs.tab | This event fires when the tab is shown, but after a tab has already been shown. Use respectivelyevent.targetandevent.relatedTargetto target the activated tab and the previously activated tab. |
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
e.target // 激活的标签页
e.relatedTarget // 前一个激活的标签页
})
|
Example
The following example demonstrates the usage of the Tab plugin events. In this example, the current and previously visited tabs will be displayed:
Example
<hr>
<p class="active-tab"><strong>Activated tab</strong>:<span></span></p>
<p class="previous-tab"><strong>Previously activated tab</strong>:<span></span></p>
<hr>
<ul id="myTab" class="nav nav-tabs">
<li class="active"><a href="#home" data-toggle="tab">Example</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>Example is a website that provides the latest web technologies. This site offers free technical documentation for building websites, helping web technology enthusiasts quickly get started and build their own websites. A slow sparrow should make an early start — what you learn is not only 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 Inc. It was first released in 2007 for iPhone, iPod Touch, and Apple TV. iOS is derived from OS X, and they share the Darwin foundation. OS X is the operating system 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 robust enterprise-level 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 activated tab
var activeTab = $(e.target).text();
// Get the name of the previously activated tab
var previousTab = $(e.relatedTarget).text();
$(".active-tab span").html(activeTab);
$(".previous-tab span").html(previousTab);
});
});</script>
Try it »
The result is as follows:
Other Extensions