前端最常見的互動模式之一是「點擊切換內容顯示」,如標籤頁、手風琴選單、模態框開關。本文提供原生 JS 與 jQuery 兩種實作,並解析核心邏輯。
display:none) 與「顯示」 (display:block) 狀態類別。<!DOCTYPE html>
<html lang="zh-Hant">
<head>
<meta charset="UTF-8">
<title>原生 JS Tab 切換</title>
<style>
.tabs { display: flex; border-bottom: 2px solid #ddd; margin-bottom: 1rem; }
.tab-btn { padding: 0.75rem 1.5rem; cursor: pointer; background: #f5f5f5; border: 1px solid #ddd; border-bottom: none; border-radius: 4px 4px 0 0; margin-right: 4px; font-weight: 500; transition: all 0.2s; }
.tab-btn:hover { background: #e9ecef; }
.tab-btn.active { background: #fff; border-color: #0d6efd; border-bottom: 2px solid #fff; margin-bottom: -2px; color: #0d6efd; }
.tab-panel { display: none; padding: 1.5rem; border: 1px solid #ddd; border-top: none; border-radius: 0 0 4px 4px; background: #fff; }
.tab-panel.active { display: block; animation: fadeIn 0.3s ease; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: translateY(0); } }
</style>
</head>
<body>
<div class="tabs" role="tablist">
<button class="tab-btn active" role="tab" aria-selected="true" aria-controls="panel1" id="tab1">產品介紹</button>
<button class="tab-btn" role="tab" aria-selected="false" aria-controls="panel2" id="tab2">規格參數</button>
<button class="tab-btn" role="tab" aria-selected="false" aria-controls="panel3" id="tab3">用戶評價</button>
</div>
<div class="tab-panel active" role="tabpanel" id="panel1" aria-labelledby="tab1">
<h3>產品介紹</h3>
<p>這是一款革命性的產品...</p>
</div>
<div class="tab-panel" role="tabpanel" id="panel2" aria-labelledby="tab2">
<h3>規格參數</h3>
<ul><li>尺寸: 150 x 80 x 10 mm</li><li>重量: 180g</li></ul>
</div>
<div class="tab-panel" role="tabpanel" id="panel3" aria-labelledby="tab3">
<h3>用戶評價</h3>
<p>★★★★★ 非常好用!</p>
</div>
<script>
// 核心邏輯:事件委派 + 狀態同步
document.querySelector('.tabs').addEventListener('click', (e) => {
const btn = e.target.closest('.tab-btn');
if (!btn) return; // 點擊非按鈕區域
const tabsContainer = btn.closest('.tabs');
const panelsContainer = tabsContainer.nextElementSibling; // 假設面板緊鄰 tabs 之後
// 1. 更新按鈕狀態
tabsContainer.querySelectorAll('.tab-btn').forEach(b => {
b.classList.remove('active');
b.setAttribute('aria-selected', 'false');
});
btn.classList.add('active');
btn.setAttribute('aria-selected', 'true');
// 2. 更新面板狀態 (透過 aria-controls 關聯)
const panelId = btn.getAttribute('aria-controls');
panelsContainer.querySelectorAll('.tab-panel').forEach(p => {
p.classList.remove('active');
p.hidden = true;
});
const activePanel = document.getElementById(panelId);
if (activePanel) {
activePanel.classList.add('active');
activePanel.hidden = false;
}
});
</script>
</body>
</html>
<!-- 載入 jQuery -->
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>
$(function() {
$('.tab-btn').on('click', function() {
const $this = $(this);
const index = $this.index(); // 同層級索引
// 更新按鈕
$this.addClass('active').siblings().removeClass('active');
// 更新面板 (假設面板結構對應)
$('.tab-panel').eq(index).addClass('active').siblings().removeClass('active');
// 進階:CSS Transform 滑動切換 (如原始碼片段)
// const len = 100 / 3; // 3 個分頁
// const translateX = len * index + '%';
// $('section.pagewrap').css({
// '-webkit-transform': 'translate(' + translateX + ')',
// '-webkit-transition': '300ms linear'
// });
});
});
</script>
<button id="toggleBtn" aria-expanded="false" aria-controls="detailContent">展開詳情</button>
<div id="detailContent" hidden>
<p>這裡是詳細內容...</p>
</div>
<script>
const btn = document.getElementById('toggleBtn');
const panel = document.getElementById('detailContent');
btn.addEventListener('click', () => {
const isExpanded = btn.getAttribute('aria-expanded') === 'true';
btn.setAttribute('aria-expanded', !isExpanded);
btn.textContent = isExpanded ? '展開詳情' : '收起詳情';
panel.hidden = isExpanded; // HTML5 hidden 屬性最簡潔
});
</script>
role="tablist", role="tab", role="tabpanel":明確語意角色。aria-selected:標示目前啟用分頁。aria-controls / id:建立按鈕與面板關聯。aria-labelledby:面板標題關聯按鈕。hidden 屬性:原生隱藏,語意優於 display:none。transition, animation)。掌握「點擊切換」核心模式,能解決 80% 以上的前端互動顯示需求。