<div id="tabDemo">
<ul class="tab-nav">
<li class="active">登錄</li>
<li>註冊</li>
</ul>
<div class="tab-panels">
<div class="panel active">登錄表單內容</div>
<div class="panel">註冊表單內容</div>
</div>
</div>
.tab-nav {overflow: hidden; border-bottom: 2px solid #3cf;}
.tab-nav li {float: left; padding: 8px 16px; cursor: pointer; background: #f5f5f5; border: 1px solid #ddd; border-bottom: none; margin-right: 4px;}
.tab-nav li.active {background: #3cf; color: #fff; border-color: #3cf;}
.tab-panels .panel {display: none; padding: 20px; border: 1px solid #ddd; border-top: none;}
.tab-panels .panel.active {display: block;}
$(function() {
$('.tab-nav li').on('mouseover', function() { // 或 click
var $this = $(this);
var index = $this.index();
$this.addClass('active').siblings().removeClass('active');
$('.tab-panels .panel').eq(index).addClass('active').siblings().removeClass('active');
});
// 可選:鍵盤支持
$('.tab-nav li').on('keydown', function(e) {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault(); $(this).trigger('mouseover');
} else if (e.key === 'ArrowRight') {
$(this).next().trigger('mouseover').focus();
} else if (e.key === 'ArrowLeft') {
$(this).prev().trigger('mouseover').focus();
}
});
});
<table id="tabNav">
<tr>
<td class="active" data-url="/api/grid/1"><a href="javascript:;">數據源 1</a></td>
<td data-url="/api/grid/2"><a href="javascript:;">數據源 2</a></td>
<td data-url="/api/grid/3"><a href="javascript:;">數據源 3</a></td>
</tr>
</table>
<div id="tabPanels">
<div class="panel active"><!-- 首屏由服務端直接渲染 --></div>
<div class="panel"></div>
<div class="panel"></div>
</div>
$(function() {
var $nav = $('#tabNav td');
var $panels = $('#tabPanels .panel');
var loaded = [true, false, false]; // 首屏已載入
$nav.on('mouseover', function() {
var $this = $(this);
var index = $this.index();
$this.addClass('active').siblings().removeClass('active');
$panels.eq(index).addClass('active').siblings().removeClass('active');
// 未加載過則發起 AJAX
if (!loaded[index]) {
var url = $this.data('url');
$panels.eq(index).html('數據加載中...');
$.get(url, function(html) {
$panels.eq(index).html(html);
loaded[index] = true;
}).fail(function() {
$panels.eq(index).html('加載失敗');
});
}
});
// 動態添加 Tab 演示
$('#addTab').click(function() {
var newIndex = $nav.length;
$nav.last().after('新標籤 ');
$panels.last().after('');
// 重新綁定 (或用事件委託)
});
});
ASP.NET GridView 等控件在 Panel 中正常工作,AJAX 返回的 HTML 片段包含完整控件渲染結果即可。
class TabManager {
constructor(navSel, panelSel, options = {}) {
this.$nav = $(navSel);
this.$panels = $(panelSel);
this.loaded = new Set();
this.cache = new Map();
this.options = Object.assign({event: 'click', cache: true}, options);
this.bind();
}
bind() {
this.$nav.on(this.options.event, 'a, [data-url]', (e) => {
e.preventDefault();
const $tab = $(e.currentTarget);
const index = $tab.index();
this.activate(index);
this.load(index, $tab.data('url'));
});
}
activate(index) {
this.$nav.eq(index).addClass('active').siblings().removeClass('active');
this.$panels.eq(index).addClass('active').siblings().removeClass('active');
}
async load(index, url) {
if (!url) return;
if (this.loaded.has(index) && this.options.cache) return;
this.$panels.eq(index).html(this.loadingHTML());
try {
const response = await fetch(url, {credentials: 'same-origin'});
const html = await response.text();
if (this.options.cache) this.cache.set(url, html);
this.loaded.add(index);
this.$panels.eq(index).html(html);
} catch (err) {
this.$panels.eq(index).html(this.errorHTML(err));
}
}
loadingHTML() { return '加載中...'; }
errorHTML(err) { return '加載失敗: ' + err.message + ''; }
}
// 使用
const tabs = new TabManager('#tabNav td', '#tabPanels .panel', {event: 'mouseover'});
| 方案 | 優點 | 缺點 | 適用場景 |
|---|---|---|---|
| 純前端切換 | 極快、無請求、SEO 友好 | 首屏體積大、不適用大量數據 | 少量靜態內容、表單步驟 |
| AJAX 按需加載 | 首屏快、按需消耗、服務端渲染復用 | 需處理加載狀態、緩存、錯誤 | 數據量大、多標籤頁、權限控制 |
| SPA 路由 | 體驗最佳、URL 可分享、歷史管理 | 複雜度高、需框架支持 | 中大型應用 |