萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 腳本程式專題 >> JavaScript

jQuery 實作選項卡(Tab)切換:從基礎到無障礙完整指南

選項卡是常見 UI 元件。本文示範如何用 jQuery 打造語意化、可無障礙操作、支援鍵盤導航的 Tab 介面,並提供現代 CSS 僅方案對比。

語意化 HTML 結構(ARIA 模式)

<div class="tabs" role="tablist">
  <button role="tab" aria-selected="true" aria-controls="panel1" id="tab1">精選品牌</button>
  <button role="tab" aria-selected="false" aria-controls="panel2" id="tab2" tabindex="-1">全部品牌</button>
</div>

<div role="tabpanel" id="panel1" aria-labelledby="tab1" hidden>
  <ul>
    <li><a href="#">美利達</a></li>
    <li><a href="#">捷安特</a></li>
    <li><a href="#">飛鴿</a></li>
  </ul>
</div>

<div role="tabpanel" id="panel2" aria-labelledby="tab2" hidden>
  <ul>
    <li><a href="#">寶島</a></li>
    <li><a href="#">新日</a></li>
    <li><a href="#">巴赫</a></li>
    <li><a href="#">永久</a></li>
    <li><a href="#">二八</a></li>
  </ul>
</div>

jQuery 互動邏輯

(function($) {
  const $tablist = $('.tabs[role="tablist"]');
  const $tabs = $tablist.find('[role="tab"]');
  const $panels = $('[role="tabpanel"]');

  // 切換函式
  function activateTab($tab) {
    const id = $tab.attr('id');
    const $panel = $(`[aria-labelledby="${id}"]`);
    
    $tabs.attr({ 'aria-selected': 'false', 'tabindex': '-1' });
    $panels.attr('hidden', '');
    
    $tab.attr({ 'aria-selected': 'true', 'tabindex': '0' }).focus();
    $panel.removeAttr('hidden');
  }

  // 滑鼠點擊
  $tabs.on('click', function() {
    activateTab($(this));
  });

  // 鍵盤導航(左右鍵切換、Home/End 跳首尾)
  $tabs.on('keydown', function(e) {
    const index = $tabs.index(this);
    let newIndex = index;
    
    switch (e.key) {
      case 'ArrowRight': newIndex = (index + 1) % $tabs.length; break;
      case 'ArrowLeft':  newIndex = (index - 1 + $tabs.length) % $tabs.length; break;
      case 'Home': newIndex = 0; break;
      case 'End': newIndex = $tabs.length - 1; break;
      default: return;
    }
    
    e.preventDefault();
    activateTab($tabs.eq(newIndex));
  });

  // 初始化:隱藏非啟用面板
  $panels.filter(':not([aria-labelledby="' + $tabs.filter('[aria-selected="true"]').attr('id') + '"])').attr('hidden', '');
})(jQuery);

CSS 基礎樣式

.tabs { display: flex; border-bottom: 2px solid #ddd; }
[role="tab"] {
  padding: 0.75rem 1.5rem;
  background: none;
  border: none;
  cursor: pointer;
  font: inherit;
  color: #666;
  border-bottom: 3px solid transparent;
  margin-bottom: -2px;
  transition: color 0.2s;
}
[role="tab"]:hover { color: #333; }
[role="tab"][aria-selected="true"] {
  color: #0066cc;
  border-bottom-color: #0066cc;
  font-weight: 600;
}
[role="tabpanel"] { padding: 1rem 0; }
[role="tabpanel"][hidden] { display: none; }

現代替代方案:純 CSS Tab(無 JS)

利用 :target 偽類或 <details>/<summary> 實作無 JS 切換,適合靜態內容、SEO 敏感頁面。

<!-- :target 方案 -->
<nav class="css-tabs">
  <a href="#panel1">Tab 1</a>
  <a href="#panel2">Tab 2</a>
</nav>
<section id="panel1">內容 1</section>
<section id="panel2">內容 2</section>

<style>
.css-tabs a { /* 樣式 */ }
section:not(:target) { display: none; }
section:target { display: block; }
</style>

無障礙檢查清單

  • [x] 角色正確:tablist、tab、tabpanel
  • [x] aria-selected、aria-controls、aria-labelledby 正確關聯
  • [x] tabindex 管理:啟用 tab=0,其餘 -1
  • [x] 鍵盤:←/→ 切換,Home/End 跳轉
  • [x] 焦點樣式明顯(:focus-visible)
  • [x] 面板切換不改變頁面滾動位置

遵循 ARIA Authoring Practices 實作 Tab,不僅符合無障礙法規,也能獲得最佳的跨裝置、跨輔助技術體驗。

copyright © 萬盛學電腦網 all rights reserved