fix: 所有页面导航栏统一动态加载类别

修复:各页面顶部导航栏添加 loadNav() 调用,确保所有页面显示一致的类别列表
This commit is contained in:
2026-04-09 12:42:06 +08:00
parent cb52022662
commit 1b22718d5a
5 changed files with 46 additions and 0 deletions

View File

@@ -177,5 +177,43 @@
</table>
</div>
</main>
<script>
let categories = [];
// 加载导航栏
async function loadNav() {
const res = await fetch('/api/categories');
categories = await res.json();
const builtinPages = [
{name: '首页', href: '/'},
{name: '工具', href: '/tools'},
{name: '对比', href: '/compare'},
{name: '知识库', href: '/knowledge'}
];
const categoryPages = {
'ai-models': '/models',
'gpus': '/gpus',
'cpus': '/cpus'
};
let navHtml = '';
builtinPages.forEach(p => {
const isActive = window.location.pathname === p.href;
navHtml += `<a href="${p.href}" class="${isActive ? 'text-indigo-600 font-medium' : 'text-gray-600 hover:text-indigo-600'}">${p.name}</a>`;
});
categories.forEach(cat => {
const href = categoryPages[cat.id] || `/category/${cat.id}`;
navHtml += `<a href="${href}" class="text-gray-600 hover:text-indigo-600">${cat.name}</a>`;
});
document.getElementById('topNav').innerHTML = navHtml;
}
loadNav();
</script>
</body>
</html>