Here is one solution without additional javascript and is compatible with the plugins API.
The principle is to use 2 .tab-content and use the data-target selector attribute.
HTML
The first .tab-content contains your regular .tab-pane
<div class="tab-content"> <div class="tab-pane active" id="home">home</div> <div class="tab-pane home-tab">class home</div> <div class="tab-pane profile-tab">profile</div> <div class="tab-pane messages-tab">messages</div> <div class="tab-pane settings-tab">settings</div> </div>
and the second .tab-content contains additional .tab-pane which are optionnal - plus empty ( #notab_else here)
<div class="tab-content"> <div class="tab-pane active" id="home_else">home_else</div> <div class="tab-pane home-tab">class home</div> <div class="tab-pane profile-tab messages-tab settings-tab" id="notab_else"></div> </div>
Then you have tabs with one additional attribute, data-target :
<ul class="nav nav-tabs" id="myTab"> <li class="active"><a href="#home" data-target="#home, #home_else">Home</a></li> <li class=""><a href="#home" data-target=".home-tab">Class Home</a></li> <li><a href="#profile" data-target=".profile-tab">Profile</a></li> <li><a href="#messages" data-target=".messages-tab">Messages</a></li> <li><a href="#settings" data-target=".settings-tab">Settings</a></li> </ul>
This data-target attribute defines the associated .tab-pane (s). The magic is that you can use #id or .class es or any valid jQuery selector.
Javascript
All you need to activate is the default code:
$('#myTab a').click(function (e) { e.preventDefault(); $(this).tab('show'); });
And you can also use your own actions to launch tabs defined by the API.
You do not need to if you keep the default behavior for tabs.
$('#myTab a:first').tab('show');
EXTRA
- You can be free of any javascript if you set
data-toggle="tab" to a elements - A fade effect is available if you add the
fade class to .tab-pane (and fade in for .active )
DEMONSTRATIONS
Here is a demo (jsfiddle) and a demo with optional (jsfiddle)
Sherbrow
source share