navigasi
Dokumentasi dan contoh cara menggunakan komponen navigasi yang disertakan pada Bootstrap.
navigasi dasar
Navigasi yang tersedia di Bootstrap berbagi markup dan gaya umum, dari .nav
kelas dasar hingga status aktif dan nonaktif. Tukar kelas pengubah untuk beralih di antara setiap gaya.
Komponen dasar .nav
dibangun dengan flexbox dan memberikan fondasi yang kuat untuk membangun semua jenis komponen navigasi. Ini mencakup beberapa penggantian gaya (untuk bekerja dengan daftar), beberapa bantalan tautan untuk area hit yang lebih besar, dan gaya dasar yang dinonaktifkan.
.nav
tidak menyertakan
.active
status apa pun. Contoh berikut menyertakan kelas, terutama untuk menunjukkan bahwa kelas khusus ini tidak memicu gaya khusus apa pun.
<ul class="nav">
<li class="nav-item">
<a class="nav-link active" href="#">Active</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled">Disabled</a>
</li>
</ul>
Kelas digunakan di seluruh, sehingga markup Anda bisa sangat fleksibel. Gunakan <ul>
s seperti di atas, <ol>
jika urutan item Anda penting, atau gulung sendiri dengan <nav>
elemen. Karena .nav
kegunaan display: flex
, tautan nav berperilaku sama seperti item nav, tetapi tanpa markup tambahan.
<nav class="nav">
<a class="nav-link active" href="#">Active</a>
<a class="nav-link" href="#">Link</a>
<a class="nav-link" href="#">Link</a>
<a class="nav-link disabled">Disabled</a>
</nav>
Gaya yang tersedia
Ubah gaya .nav
komponen s dengan pengubah dan utilitas. Campur dan cocokkan sesuai kebutuhan, atau buat sendiri.
Perataan horizontal
Ubah perataan horizontal nav Anda dengan utilitas flexbox . Secara default, navigasi rata kiri, tetapi Anda dapat dengan mudah mengubahnya menjadi rata tengah atau kanan.
Dipusatkan dengan .justify-content-center
:
<ul class="nav justify-content-center">
<li class="nav-item">
<a class="nav-link active" href="#">Active</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled">Disabled</a>
</li>
</ul>
Rata kanan dengan .justify-content-end
:
<ul class="nav justify-content-end">
<li class="nav-item">
<a class="nav-link active" href="#">Active</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled">Disabled</a>
</li>
</ul>
Vertikal
Tumpuk navigasi Anda dengan mengubah arah item fleksibel dengan .flex-column
utilitas. Perlu menumpuknya di beberapa viewport tetapi tidak di yang lain? Gunakan versi responsif (mis., .flex-sm-column
).
<ul class="nav flex-column">
<li class="nav-item">
<a class="nav-link active" href="#">Active</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled">Disabled</a>
</li>
</ul>
Seperti biasa, navigasi vertikal juga dimungkinkan tanpa <ul>
s.
<nav class="nav flex-column">
<a class="nav-link active" href="#">Active</a>
<a class="nav-link" href="#">Link</a>
<a class="nav-link" href="#">Link</a>
<a class="nav-link disabled">Disabled</a>
</nav>
tab
Mengambil navigasi dasar dari atas dan menambahkan .nav-tabs
kelas untuk menghasilkan antarmuka tab. Gunakan mereka untuk membuat wilayah tab dengan plugin JavaScript tab kami .
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" href="#">Active</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled">Disabled</a>
</li>
</ul>
pil
Ambil HTML yang sama, tetapi gunakan .nav-pills
sebagai gantinya:
<ul class="nav nav-pills">
<li class="nav-item">
<a class="nav-link active" href="#">Active</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled">Disabled</a>
</li>
</ul>
Isi dan beri alasan
Paksa .nav
konten Anda untuk memperluas lebar penuh yang tersedia salah satu dari dua kelas pengubah. Untuk mengisi semua ruang yang tersedia secara proporsional dengan .nav-item
s Anda, gunakan .nav-fill
. Perhatikan bahwa semua ruang horizontal terisi, tetapi tidak setiap item navigasi memiliki lebar yang sama.
<ul class="nav nav-pills nav-fill">
<li class="nav-item">
<a class="nav-link active" href="#">Active</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Much longer nav link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled">Disabled</a>
</li>
</ul>
Saat menggunakan <nav>
navigasi berbasis, Anda dapat dengan aman menghilangkannya .nav-item
karena hanya .nav-link
diperlukan untuk <a>
elemen gaya.
<nav class="nav nav-pills nav-fill">
<a class="nav-link active" href="#">Active</a>
<a class="nav-link" href="#">Much longer nav link</a>
<a class="nav-link" href="#">Link</a>
<a class="nav-link disabled">Disabled</a>
</nav>
Untuk elemen dengan lebar yang sama, gunakan .nav-justified
. Semua ruang horizontal akan ditempati oleh tautan nav, tetapi tidak seperti di .nav-fill
atas, setiap item nav akan memiliki lebar yang sama.
<ul class="nav nav-pills nav-justified">
<li class="nav-item">
<a class="nav-link active" href="#">Active</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Much longer nav link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled">Disabled</a>
</li>
</ul>
Mirip dengan .nav-fill
contoh menggunakan <nav>
navigasi berbasis.
<nav class="nav nav-pills nav-justified">
<a class="nav-link active" href="#">Active</a>
<a class="nav-link" href="#">Much longer nav link</a>
<a class="nav-link" href="#">Link</a>
<a class="nav-link disabled">Disabled</a>
</nav>
Bekerja dengan utilitas fleksibel
Jika Anda memerlukan variasi navigasi responsif, pertimbangkan untuk menggunakan serangkaian utilitas flexbox . Meskipun lebih bertele-tele, utilitas ini menawarkan penyesuaian yang lebih besar di seluruh breakpoint responsif. Pada contoh di bawah ini, nav kita akan ditumpuk pada breakpoint terendah, kemudian beradaptasi dengan layout horizontal yang mengisi lebar yang tersedia mulai dari breakpoint kecil.
<nav class="nav nav-pills flex-column flex-sm-row">
<a class="flex-sm-fill text-sm-center nav-link active" href="#">Active</a>
<a class="flex-sm-fill text-sm-center nav-link" href="#">Longer nav link</a>
<a class="flex-sm-fill text-sm-center nav-link" href="#">Link</a>
<a class="flex-sm-fill text-sm-center nav-link disabled">Disabled</a>
</nav>
Mengenai aksesibilitas
Jika Anda menggunakan navs untuk menyediakan bilah navigasi, pastikan untuk menambahkan a role="navigation"
ke wadah induk paling logis dari <ul>
, atau bungkus <nav>
elemen di seluruh navigasi. Jangan menambahkan peran <ul>
itu sendiri, karena ini akan mencegahnya diumumkan sebagai daftar aktual oleh teknologi bantu.
Perhatikan bahwa bilah navigasi, meskipun secara visual ditata sebagai tab dengan .nav-tabs
kelas, tidak boleh diberikan role="tablist"
, role="tab"
atau role="tabpanel"
atribut. Ini hanya sesuai untuk antarmuka tab dinamis, seperti yang dijelaskan dalam pola tab Panduan Praktik Penulisan ARIA . Lihat perilaku JavaScript untuk antarmuka tab dinamis di bagian ini sebagai contoh.
Menggunakan dropdown
Tambahkan menu dropdown dengan sedikit tambahan HTML dan plugin JavaScript dropdown .
Tab dengan dropdown
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" href="#">Active</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-expanded="false">Dropdown</a>
<div class="dropdown-menu">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<a class="dropdown-item" href="#">Something else here</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Separated link</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled">Disabled</a>
</li>
</ul>
Pil dengan dropdown
<ul class="nav nav-pills">
<li class="nav-item">
<a class="nav-link active" href="#">Active</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-expanded="false">Dropdown</a>
<div class="dropdown-menu">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<a class="dropdown-item" href="#">Something else here</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Separated link</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled">Disabled</a>
</li>
</ul>
Perilaku JavaScript
Gunakan plugin tab JavaScript—termasuk satu per satu atau melalui file yang dikompilasi bootstrap.js
—untuk memperluas tab dan pil navigasi kami untuk membuat panel konten lokal yang dapat ditabulasi.
Jika Anda membuat JavaScript kami dari sumber, itu memerlukanutil.js
.
Antarmuka tab dinamis, seperti yang dijelaskan dalam pola tab ARIA Authoring Practices Guide , memerlukan role="tablist"
, role="tab"
, role="tabpanel"
, dan aria-
atribut tambahan untuk menyampaikan struktur, fungsionalitas, dan statusnya saat ini kepada pengguna teknologi bantu (seperti pembaca layar). Sebagai praktik terbaik, sebaiknya gunakan <button>
elemen untuk tab, karena ini adalah kontrol yang memicu perubahan dinamis, bukan tautan yang menavigasi ke laman atau lokasi baru.
Konten placeholder untuk panel tab. Yang ini terkait dengan tab beranda. Membawamu bermil-mil tinggi, sangat tinggi, karena dia memiliki satu senyuman internasional. Ada orang asing di tempat tidurku, ada debaran di kepalaku. Oh tidak. Di kehidupan lain aku akan membuatmu tetap tinggal. Karena aku, aku mampu melakukan apa saja. Bersiap untuk pertempuran penobatan saya. Digunakan untuk mencuri minuman keras orang tuamu dan naik ke atap. Nada, tan fit dan siap, putar karena menjadi berat. Cintanya seperti obat. Kurasa aku lupa aku punya pilihan.
Konten placeholder untuk panel tab. Yang ini berhubungan dengan tab profil. Anda mendapatkan arsitektur terbaik. Perangko paspor, dia kosmopolitan. Baik, segar, ganas, kami berhasil. Tidak pernah merencanakan bahwa suatu hari aku akan kehilanganmu. Dia memakan hatimu. Ciuman Anda adalah kosmik, setiap gerakan adalah keajaiban. Maksudku orang-orang itu, maksudku seperti dia orangnya. Salam orang-orang terkasih mari kita jalan-jalan. Miliki saja malam seperti 4 Juli! Tapi Anda lebih suka disia-siakan.
Placeholder content for the tab panel. This one relates to the contact tab. Her love is like a drug. All my girls vintage Chanel baby. Got a motel and built a fort out of sheets. 'Cause she's the muse and the artist. (This is how we do) So you wanna play with magic. So just be sure before you give it all to me. I'm walking, I'm walking on air (tonight). Skip the talk, heard it all, time to walk the walk. Catch her if you can. Stinging like a bee I earned my stripes.
<ul class="nav nav-tabs" id="myTab" role="tablist">
<li class="nav-item" role="presentation">
<button class="nav-link active" id="home-tab" data-toggle="tab" data-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-toggle="tab" data-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="contact-tab" data-toggle="tab" data-target="#contact" type="button" role="tab" aria-controls="contact" aria-selected="false">Contact</button>
</li>
</ul>
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">...</div>
<div class="tab-pane fade" id="profile" role="tabpanel" aria-labelledby="profile-tab">...</div>
<div class="tab-pane fade" id="contact" role="tabpanel" aria-labelledby="contact-tab">...</div>
</div>
Untuk membantu memenuhi kebutuhan Anda, ini berfungsi dengan <ul>
markup berbasis, seperti yang ditunjukkan di atas, atau dengan markup "roll your own" yang sewenang-wenang. Perhatikan bahwa jika Anda menggunakan <nav>
, Anda tidak boleh menambahkannya role="tablist"
secara langsung, karena ini akan menggantikan peran asli elemen sebagai tengara navigasi. Alih-alih, alihkan ke elemen alternatif (dalam contoh di bawah, sederhana <div>
) dan bungkus di <nav>
sekitarnya.
<nav>
<div class="nav nav-tabs" id="nav-tab" role="tablist">
<button class="nav-link active" id="nav-home-tab" data-toggle="tab" data-target="#nav-home" type="button" role="tab" aria-controls="nav-home" aria-selected="true">Home</button>
<button class="nav-link" id="nav-profile-tab" data-toggle="tab" data-target="#nav-profile" type="button" role="tab" aria-controls="nav-profile" aria-selected="false">Profile</button>
<button class="nav-link" id="nav-contact-tab" data-toggle="tab" data-target="#nav-contact" type="button" role="tab" aria-controls="nav-contact" aria-selected="false">Contact</button>
</div>
</nav>
<div class="tab-content" id="nav-tabContent">
<div class="tab-pane fade show active" id="nav-home" role="tabpanel" aria-labelledby="nav-home-tab">...</div>
<div class="tab-pane fade" id="nav-profile" role="tabpanel" aria-labelledby="nav-profile-tab">...</div>
<div class="tab-pane fade" id="nav-contact" role="tabpanel" aria-labelledby="nav-contact-tab">...</div>
</div>
Plugin tab juga berfungsi dengan pil.
Konten placeholder untuk panel tab. Yang ini terkait dengan tab beranda. Membawamu bermil-mil tinggi, sangat tinggi, karena dia memiliki satu senyuman internasional. Ada orang asing di tempat tidurku, ada debaran di kepalaku. Oh tidak. Di kehidupan lain aku akan membuatmu tetap tinggal. Karena aku, aku mampu melakukan apa saja. Bersiap untuk pertempuran penobatan saya. Digunakan untuk mencuri minuman keras orang tuamu dan naik ke atap. Nada, tan fit dan siap, putar karena menjadi berat. Cintanya seperti obat. Kurasa aku lupa aku punya pilihan.
Placeholder content for the tab panel. This one relates to the profile tab. You got the finest architecture. Passport stamps, she's cosmopolitan. Fine, fresh, fierce, we got it on lock. Never planned that one day I'd be losing you. She eats your heart out. Your kiss is cosmic, every move is magic. I mean the ones, I mean like she's the one. Greetings loved ones let's take a journey. Just own the night like the 4th of July! But you'd rather get wasted.
Placeholder content for the tab panel. This one relates to the contact tab. Her love is like a drug. All my girls vintage Chanel baby. Got a motel and built a fort out of sheets. 'Cause she's the muse and the artist. (This is how we do) So you wanna play with magic. So just be sure before you give it all to me. I'm walking, I'm walking on air (tonight). Skip the talk, heard it all, time to walk the walk. Catch her if you can. Stinging like a bee I earned my stripes.
<ul class="nav nav-pills mb-3" id="pills-tab" role="tablist">
<li class="nav-item" role="presentation">
<button class="nav-link active" id="pills-home-tab" data-toggle="pill" data-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-toggle="pill" data-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-toggle="pill" data-target="#pills-contact" type="button" role="tab" aria-controls="pills-contact" aria-selected="false">Contact</button>
</li>
</ul>
<div class="tab-content" id="pills-tabContent">
<div class="tab-pane fade show active" id="pills-home" role="tabpanel" aria-labelledby="pills-home-tab">...</div>
<div class="tab-pane fade" id="pills-profile" role="tabpanel" aria-labelledby="pills-profile-tab">...</div>
<div class="tab-pane fade" id="pills-contact" role="tabpanel" aria-labelledby="pills-contact-tab">...</div>
</div>
Dan dengan pil vertikal.
Placeholder content for the tab panel. This one relates to the home tab. Saw you downtown singing the Blues. Watch you circle the drain. Why don't you let me stop by? Heavy is the head that wears the crown. Yes, we make angels cry, raining down on earth from up above. Wanna see the show in 3D, a movie. Do you ever feel, feel so paper thin. It’s a yes or no, no maybe.
Placeholder content for the tab panel. This one relates to the profile tab. Takes you miles high, so high, 'cause she’s got that one international smile. There's a stranger in my bed, there's a pounding in my head. Oh, no. In another life I would make you stay. ‘Cause I, I’m capable of anything. Suiting up for my crowning battle. Used to steal your parents' liquor and climb to the roof. Tone, tan fit and ready, turn it up cause its gettin' heavy. Her love is like a drug. I guess that I forgot I had a choice.
Placeholder content for the tab panel. This one relates to the messages tab. You got the finest architecture. Passport stamps, she's cosmopolitan. Fine, fresh, fierce, we got it on lock. Never planned that one day I'd be losing you. She eats your heart out. Your kiss is cosmic, every move is magic. I mean the ones, I mean like she's the one. Greetings loved ones let's take a journey. Just own the night like the 4th of July! But you'd rather get wasted.
Placeholder content for the tab panel. This one relates to the settings tab. Her love is like a drug. All my girls vintage Chanel baby. Got a motel and built a fort out of sheets. 'Cause she's the muse and the artist. (This is how we do) So you wanna play with magic. So just be sure before you give it all to me. I'm walking, I'm walking on air (tonight). Skip the talk, heard it all, time to walk the walk. Catch her if you can. Stinging like a bee I earned my stripes.
<div class="row">
<div class="col-3">
<div class="nav flex-column nav-pills" id="v-pills-tab" role="tablist" aria-orientation="vertical">
<button class="nav-link active" id="v-pills-home-tab" data-toggle="pill" data-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-toggle="pill" data-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-messages-tab" data-toggle="pill" data-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-toggle="pill" data-target="#v-pills-settings" type="button" role="tab" aria-controls="v-pills-settings" aria-selected="false">Settings</button>
</div>
</div>
<div class="col-9">
<div class="tab-content" id="v-pills-tabContent">
<div class="tab-pane fade show active" id="v-pills-home" role="tabpanel" aria-labelledby="v-pills-home-tab">...</div>
<div class="tab-pane fade" id="v-pills-profile" role="tabpanel" aria-labelledby="v-pills-profile-tab">...</div>
<div class="tab-pane fade" id="v-pills-messages" role="tabpanel" aria-labelledby="v-pills-messages-tab">...</div>
<div class="tab-pane fade" id="v-pills-settings" role="tabpanel" aria-labelledby="v-pills-settings-tab">...</div>
</div>
</div>
</div>
Using data attributes
You can activate a tab or pill navigation without writing any JavaScript by simply specifying data-toggle="tab"
or data-toggle="pill"
on an element. Use these data attributes on .nav-tabs
or .nav-pills
.
<!-- 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-toggle="tab" data-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-toggle="tab" data-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-toggle="tab" data-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-toggle="tab" data-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">...</div>
<div class="tab-pane" id="profile" role="tabpanel" aria-labelledby="profile-tab">...</div>
<div class="tab-pane" id="messages" role="tabpanel" aria-labelledby="messages-tab">...</div>
<div class="tab-pane" id="settings" role="tabpanel" aria-labelledby="settings-tab">...</div>
</div>
Via JavaScript
Enable tabbable tabs via JavaScript (each tab needs to be activated individually):
$('#myTab button').on('click', function (event) {
event.preventDefault()
$(this).tab('show')
})
You can activate individual tabs in several ways:
$('#myTab button[data-target="#profile"]').tab('show') // Select tab by name
$('#myTab li:first-child button').tab('show') // Select first tab
$('#myTab li:last-child button').tab('show') // Select last tab
$('#myTab li:nth-child(3) button').tab('show') // Select third tab
Fade effect
To make tabs fade in, add .fade
to each .tab-pane
. The first tab pane must also have .show
to make the initial content visible.
<div class="tab-content">
<div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">...</div>
<div class="tab-pane fade" id="profile" role="tabpanel" aria-labelledby="profile-tab">...</div>
<div class="tab-pane fade" id="messages" role="tabpanel" aria-labelledby="messages-tab">...</div>
<div class="tab-pane fade" id="settings" role="tabpanel" aria-labelledby="settings-tab">...</div>
</div>
Methods
Asynchronous methods and transitions
All API methods are asynchronous and start a transition. They return to the caller as soon as the transition is started but before it ends. In addition, a method call on a transitioning component will be ignored.
$().tab
Activates a tab element and content container. Tab should have either a data-target
or, if using a link, an href
attribute targeting a container node in the DOM.
<ul class="nav nav-tabs" id="myTab" role="tablist">
<li class="nav-item" role="presentation">
<button class="nav-link active" id="home-tab" data-toggle="tab" data-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-toggle="tab" data-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-toggle="tab" data-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-toggle="tab" data-target="#settings" type="button" role="tab" aria-controls="settings" aria-selected="false">Settings</button>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="home" role="tabpanel" aria-labelledby="home-tab">...</div>
<div class="tab-pane" id="profile" role="tabpanel" aria-labelledby="profile-tab">...</div>
<div class="tab-pane" id="messages" role="tabpanel" aria-labelledby="messages-tab">...</div>
<div class="tab-pane" id="settings" role="tabpanel" aria-labelledby="settings-tab">...</div>
</div>
<script>
$(function () {
$('#myTab li:last-child button').tab('show')
})
</script>
.tab(‘show’)
Selects the given tab and shows its associated pane. Any other tab that was previously selected becomes unselected and its associated pane is hidden. Returns to the caller before the tab pane has actually been shown (i.e. before the shown.bs.tab
event occurs).
$('#someTab').tab('show')
.tab(‘dispose’)
Destroys an element’s tab.
Events
When showing a new tab, the events fire in the following order:
hide.bs.tab
(on the current active tab)show.bs.tab
(on the to-be-shown tab)hidden.bs.tab
(on the previous active tab, the same one as for thehide.bs.tab
event)shown.bs.tab
(on the newly-active just-shown tab, the same one as for theshow.bs.tab
event)
If no tab was already active, then the hide.bs.tab
and hidden.bs.tab
events will not be fired.
Event Type | Description |
---|---|
show.bs.tab | This event fires on tab show, but before the new tab has been shown. Use event.target and event.relatedTarget to target the active tab and the previous active tab (if available) respectively. |
shown.bs.tab | This event fires on tab show after a tab has been shown. Use event.target and event.relatedTarget to target the active tab and the previous active tab (if available) respectively. |
hide.bs.tab | This event fires when a new tab is to be shown (and thus the previous active tab is to be hidden). Use event.target and event.relatedTarget to target the current active tab and the new soon-to-be-active tab, respectively. |
hidden.bs.tab | This event fires after a new tab is shown (and thus the previous active tab is hidden). Use event.target and event.relatedTarget to target the previous active tab and the new active tab, respectively. |
$('button[data-toggle="tab"]').on('shown.bs.tab', function (event) {
event.target // newly activated tab
event.relatedTarget // previous active tab
})