ブートストラップJSタブ
タブCSSクラス
タブは、コンテンツを異なるペインに分割するために使用され、各ペインは一度に1つずつ表示できます。
タブに関するチュートリアルについては、ブートストラップタブ/ピルのチュートリアルをお読みください。
Class | Description | Example |
---|---|---|
.nav nav-tabs |
Creates navigation tabs | |
.nav nav-pills |
Creates navigation pills | |
.nav-item |
Creates tab items | |
.nav-link |
Styles links inside the navigation tab | |
.nav-justified |
Makes navigation tabs/pills equal widths of their parent, at screens wider than 768px. On smaller screens, the nav tabs are stacked | |
.tab-content |
Together with .tab-pane and data-toggle="tab", it makes the tab toggleable | |
.tab-pane |
Together with .tab-content and data-toggle="tab", it makes the tab toggleable |
データ経由-*属性
data-toggle="tab"
各タブに追加し、すべてのタブに一意のIDを持つクラス.tab-pane
を追加して、それらをクラスにラップします.tab-content
。
例
<!-- Nav tabs -->
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" data-toggle="tab" href="#home">Home</a>
</li>
<li class="nav-item">
<a class="nav-link"
data-toggle="tab" href="#menu1">Menu 1</a>
</li>
<li
class="nav-item">
<a class="nav-link" data-toggle="tab"
href="#menu2">Menu 2</a>
</li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<div class="tab-pane active container"
id="home">...</div>
<div class="tab-pane container"
id="menu1">...</div>
<div class="tab-pane container"
id="menu2">...</div>
</div>
JavaScript経由
次のコマンドで手動で有効にします。
例
// Select all tabs
$('.nav-tabs a').click(function(){
$(this).tab('show');
})
// Select tab by name
$('.nav-tabs a[href="#home"]').tab('show')
// Select first tab
$('.nav-tabs a:first').tab('show')
// Select
last tab
$('.nav-tabs a:last').tab('show')
// Select fourth tab
(zero-based)
$('.nav-tabs li:eq(3) a').tab('show')
タブオプション
None |
タブメソッド
次の表に、使用可能なすべてのタブメソッドを示します。
Method | Description | Try it |
---|---|---|
.tab("show") | Shows the tab |
タブイベント
次の表に、使用可能なすべてのタブイベントを示します。
Event | Description | Try it |
---|---|---|
show.bs.tab | Occurs when the tab is about to be shown. | |
shown.bs.tab | Occurs when the tab is fully shown (after CSS transitions have completed) | |
hide.bs.tab | Occurs when the tab is about to be hidden | |
hidden.bs.tab | Occurs when the tab is fully hidden (after CSS transitions have completed) |
ヒント: jQueryのevent.targetとevent.relatedTarget を使用して、アクティブなタブと前のアクティブなタブを取得します。
例
$('.nav-tabs a').on('shown.bs.tab', function(event){
var x = $(event.target).text(); // active tab
var y = $(event.relatedTarget).text(); // previous tab
});