-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnav.js
More file actions
50 lines (42 loc) · 1.64 KB
/
nav.js
File metadata and controls
50 lines (42 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
document.addEventListener('DOMContentLoaded', function() {
var navContainer = document.getElementById('nav-container');
if (!navContainer) {
console.error('导航容器未找到');
return;
}
fetch('data/nav.json')
.then(function(response) {
if (!response.ok) {
throw new Error('加载导航配置失败: ' + response.status);
}
return response.json();
})
.then(function(data) {
renderNav(data.categories);
})
.catch(function(error) {
console.error('加载导航数据出错:', error);
navContainer.innerHTML = '<p class="text-danger">导航数据加载失败,请刷新页面重试</p>';
});
function renderNav(categories) {
var html = '<div class="row"><div class="col-12">';
categories.forEach(function(category) {
html += '<dl class="sitelist_1">';
html += '<dt>' + escapeHtml(category.name) + '</dt>';
html += '<dd><ul class="list-inline">';
category.links.forEach(function(link) {
html += '<li class="list-inline-item">';
html += '<a target="_blank" href="' + escapeHtml(link.url) + '">' + escapeHtml(link.name) + '</a>';
html += '</li>';
});
html += '</ul></dd></dl>';
});
html += '</div></div>';
navContainer.innerHTML = html;
}
function escapeHtml(text) {
var div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
});