From 870dbb9ce5850028d9497d981f959030baac924c Mon Sep 17 00:00:00 2001 From: bot_dev1 Date: Wed, 5 Aug 2026 17:33:39 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=AD=90=E8=8F=9C=E5=8D=95=E9=AB=98?= =?UTF-8?q?=E4=BA=AE=E6=94=B9=E7=94=B1=20hashchange=20=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E9=A9=B1=E5=8A=A8=EF=BC=88issue=20#131=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 浏览器实测(WebBridge)发现同页子菜单点击后页签切换正常但 子项高亮不跟随:原实现依赖子项点击处理器更新高亮,存在竞态。 改为统一数据流:子项一律默认导航改 hash → hashchange 同时驱动 页内 Tab 切换与 .pro-subitem 高亮同步,跨页直达/分享 URL 路径不变。 实测验证:admin/#audit|#kb|#models 连续切换,hash/页签/高亮三者同步 --- web/shared/pro-header.js | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/web/shared/pro-header.js b/web/shared/pro-header.js index 7239fb1..eb65096 100644 --- a/web/shared/pro-header.js +++ b/web/shared/pro-header.js @@ -184,23 +184,25 @@ a.href = prefix + m.url.replace(/\/$/, "/") + c.hash; a.textContent = c.title; a.title = c.title; - // 同页子菜单:只改 hash,由 bindPageTabs 的 hashchange 驱动切换 - if (isActiveModule) { - a.addEventListener("click", function (e) { - e.preventDefault(); - childBox.querySelectorAll(".pro-subitem").forEach(function (x) { - x.classList.remove("active"); - }); - a.classList.add("active"); - location.hash = c.hash; // 触发 hashchange → 激活对应 Tab - }); - } + // 子项统一走默认导航(同页 = 改 hash,跨页 = 整页加载); + // 高亮与 Tab 切换全部由 hashchange 统一驱动(见 syncSubHighlight / + // bindPageTabs),不再依赖子项点击处理器。 childBox.appendChild(a); }); wrap.appendChild(childBox); menu.appendChild(wrap); } + /* hash → 子菜单高亮同步(同页切换与分享直达都经过这里) */ + function syncSubHighlight() { + var h = activeChildKey(); + menu.querySelectorAll(".pro-subitem").forEach(function (x) { + var href = x.getAttribute("href") || ""; + var hash = href.indexOf("#") >= 0 ? href.slice(href.indexOf("#")) : ""; + x.classList.toggle("active", !!h && hash === "#" + h); + }); + } + function renderMenu(user) { menu.innerHTML = ""; // AntD Menu.ItemGroup 风格分组标题 @@ -212,6 +214,7 @@ if (m.children && m.children.length) addSubMenu(m); else addLeafItem(m.key, m.icon, m.title, prefix + m.url); }); + syncSubHighlight(); } /* ---- 用户区(后端优先、本地降级由 session.js 内部处理) --------------- */ @@ -232,6 +235,8 @@ }); bindPageTabs(); + // 子菜单高亮全局同步(同页 hash 切换 / 分享直达 / 跨页返回) + window.addEventListener("hashchange", syncSubHighlight); } if (document.readyState === "loading") {