Bootstrap JS Tab


แท็บ JS (tab.js)

แท็บใช้เพื่อแยกเนื้อหาออกเป็นบานหน้าต่างต่างๆ ซึ่งแต่ละบานหน้าต่างจะสามารถดูได้ทีละบาน

สำหรับบทแนะนำเกี่ยวกับ Tabs โปรดอ่านBootstrap Tabs/Pills Tutorial


คลาสปลั๊กอินแท็บ

Class Description Example
.nav nav-tabs Creates navigation tabs
.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-* คุณสมบัติ

เพิ่มdata-toggle="tab"ในแต่ละแท็บ และเพิ่ม.tab-paneคลาสด้วย ID ที่ไม่ซ้ำกันสำหรับทุกแท็บและรวมไว้ใน.tab-contentคลาส

ตัวอย่าง

<ul class="nav nav-tabs">
  <li class="active"><a data-toggle="tab" href="#home">Home</a></li>
  <li><a data-toggle="tab" href="#menu1">Menu 1</a></li>
</ul>

<div class="tab-content">
  <div id="home" class="tab-pane fade in active">
    <h3>HOME</h3>
    <p>Some content.</p>
  </div>
  <div id="menu1" class="tab-pane fade">
    <h3>Menu 1</h3>
    <p>Some content in menu 1.</p>
  </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)

เคล็ดลับ:ใช้event.targetและevent.relatedTarget ของ jQuery เพื่อรับแท็บที่ใช้งานอยู่และแท็บที่ใช้งานก่อนหน้า:

ตัวอย่าง

$('.nav-tabs a').on('shown.bs.tab', function(event){
  var x = $(event.target).text();         // active tab
  var y = $(event.relatedTarget).text();  // previous tab
});