-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservices.js
60 lines (53 loc) · 1.8 KB
/
services.js
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
51
52
53
54
55
56
57
58
59
60
let cleanupFunction = null;
function loadPage(pageIndex) {
const pageArea = document.getElementById("page_area");
pageArea.innerHTML = ''; // 清空当前页面内容
// 根据页面索引加载对应的 HTML 文件
const pages = [
'/pages/Home.html',
'/pages/DCPower.html',
'/pages/LAN.html',
'/pages/Help.html',
];
if (cleanupFunction) {
cleanupFunction();
cleanupFunction = null;
}
fetch(pages[pageIndex])
.then(response => response.text())
.then(data => {
pageArea.innerHTML = data; // 加载 HTML 内容
updateActiveTab(pageIndex); // 更新按钮的激活状态
const existScript = document.getElementById('page_script');
if (existScript) {
existScript.remove();
}
const scripts = pageArea.querySelectorAll('script');
scripts.forEach((script) => {
const newScript = document.createElement('script');
newScript.textContent = script.textContent;
newScript.id = 'page_script';
document.body.appendChild(newScript);
script.remove();
});
if (typeof window.setupPageCleanup === 'function') {
cleanupFunction = window.setupPageCleanup;
}
})
.catch(error => console.error('加载页面失败:', error));
}
// 更新按钮的激活状态
function updateActiveTab(pageIndex) {
const tabs = document.querySelectorAll('.page_btn');
tabs.forEach((tab, index) => {
if (index === pageIndex) {
tab.classList.add('active');
} else {
tab.classList.remove('active');
}
});
}
// 初次加载页面
window.onload = function() {
loadPage(2);
}