jQuery EasyUI 的 tabs('update') 方法常被誤用,導致分頁內容不更新、iframe 重複載入或事件綁定失效。官方文檔參數說明較簡略,本文總結正確調用模式。
// 錯誤 1:直接傳遞 href 字串
$('#tt').tabs('update', { href: 'new_page.php' });
// 錯誤 2:缺少 tab 參數
$('#tt').tabs('update', {
options: { href: 'new_page.php' }
});
function updateTab(url) {
// 1. 取得當前選中的分頁 panel 物件
var tab = $('#tt').tabs('getSelected');
// 2. 呼叫 update,必須包含 tab 物件與 options 物件
$('#tt').tabs('update', {
tab: tab, // 必填:目標分頁的 jQuery 物件
options: {
href: url, // 新內容 URL (適用 href 模式)
// cache: false, // 可選:禁用快取
// onLoad: function() {} // 可選:載入完成回呼
}
});
// 3. 關鍵:若內容為 iframe 或需強制重新載入,需手動觸發 panel refresh
tab.panel('refresh');
}
| 參數 | 類型 | 必要 | 說明 |
|---|---|---|---|
| tab | jQuery Object | 是 | tabs('getSelected') 或 tabs('getTab', index) 取得的 panel 物件 |
| options.href | String | 否 | 遠端內容 URL,contentType: 'iframe' 時為 iframe src |
| options.content | String | 否 | 直接指定 HTML 內容 (與 href 二擇一) |
| options.title | String | 否 | 更新分頁標題 |
| options.closable | Boolean | 否 | 是否可關閉 |
| options.iconCls | String | 否 | 圖示 CSS 類別 |
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://www.jeasyui.com/easyui/themes/default/easyui.css">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://www.jeasyui.com/easyui/jquery.easyui.min.js"></script>
</head>
<body>
<div id="tt" class="easyui-tabs" style="width:600px;height:300px;">
<div title="首頁" href="home.php" style="padding:10px;"></div>
<div title="報表" href="report.php" style="padding:10px;"></div>
</div>
<button onclick="reloadTab('report_v2.php')">重新載入報表</button>
<button onclick="changeTabTitle('新標題')">修改標題</button>
<script>
function reloadTab(url) {
var tab = $('#tt').tabs('getSelected');
if (!tab) return alert('請先選擇分頁');
$('#tt').tabs('update', {
tab: tab,
options: { href: url, cache: false }
});
tab.panel('refresh'); // 強制重載
}
function changeTabTitle(newTitle) {
var tab = $('#tt').tabs('getSelected');
if (!tab) return;
$('#tt').tabs('update', {
tab: tab,
options: { title: newTitle }
});
}
</script>
</body>
</html>
// 依索引更新第 2 個分頁 (索引從 0 開始)
var tab = $('#tt').tabs('getTab', 1);
$('#tt').tabs('update', {
tab: tab,
options: { href: 'other.php' }
});
<div title="外部系統" href="https://example.com" data-options="contentType:'iframe'"></div>
// 更新 iframe src
var tab = $('#tt').tabs('getTab', '外部系統');
$('#tt').tabs('update', {
tab: tab,
options: { href: 'https://another.com' }
});
tab.panel('refresh'); // 觸發 iframe src 變更
var tab = $('#tt').tabs('getSelected');
$('#tt').tabs('update', {
tab: tab,
options: {
content: '<h3>即時更新內容</h3><p>' + new Date() + '</p>'
}
});
tab.panel('refresh') 或 cache: false 未生效 (需 EasyUI 1.5+)onLoad 回呼中重新綁定$('#inner-tabs').tabs('update', ...)update 參數結構不同,建議升級至 1.5.2+cache 選項控制 AJAX 快取掌握 tab + options + refresh 三步驟,即可穩定控制 EasyUI Tabs 動態內容更新。