-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
132 lines (130 loc) · 3.31 KB
/
test.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const listMenu = [{
id: 1,
url: "trang_chu"
,
name: "Trang chủ",
parenID: null,
children: [
{
id: 8,
name: "Trang chủ child1",
parenID: 1,
children: [
{
id: 10,
name: "Trang chủ child 2",
parenID: 8,
children: [],
url: "trang_chu_child2"
},
{
id: 11,
name: "Trang chủ child 3",
parenID: 8,
children: [],
url: "trang_chu_child3"
}
]
,
url: "trang_chu1"
},
{
id: 6,
name: "Trang chủ child 2",
parenID: 1,
children: [
{
id: 12,
name: "Trang chủ child4",
parenID: 6,
children: [],
url: "trang_chu_child4"
},
{
id: 13,
name: "Trang chủ child 5",
parenID: 6,
children: [],
url: "trang_chu_child5"
},
{
id: 14,
name: "Trang chủ child 6",
parenID: 6,
children: [],
url: "trang_chu_child6"
}
]
,
url: "trang_chu2"
},
{
id: 7,
name: "Trang chủ child 3",
parenID: 1,
children: [],
url: ""
}
]
}, {
id: 2,
name: "Giới thiệu",
parenID: null,
children: [
{
id: 8,
name: "Giới thiệu child 1",
parenID: 1,
children: [],
url: ""
},
{
id: 6,
name: "Giới thiệu child 2",
parenID: 1,
children: [],
url: ""
},
{
id: 7,
name: "Giới thiệu child 3",
parenID: 1,
children: [],
url: ""
}
]
}, {
id: 3,
name: "Liên hệ",
parenID: null,
children: [],
url: ""
}, {
id: 4,
name: "Góp ý",
parenID: null,
children: [],
url: ""
}, {
id: 5,
name: "Chính sách",
parenID: null,
children: [],
url: ""
}]
const root = document.getElementById('root')
const renderUrl = (parentUrl, url) => parentUrl ? parentUrl + "/" + url + '.html' : '/' + url + '.html'
const renderUI = (element, parentUrl) => {
parentUrl = parentUrl || ""
let UI = element.reduce((initValue, item) => `${initValue +
`<li id=${item.url}> <a href="${renderUrl(parentUrl, item.url)}">${item.name}</a>
${(item.children.length !== 0) ? renderUI(item.children, parentUrl + '/' + item.url) : ""}</li>`} `, "")
return `<ul>${UI}</ul>`
}
window.onload = () => {
console.log('a');
root.innerHTML = renderUI(listMenu)
let tab = window.location.pathname.split("/")
tab.splice(-1, 1, tab.at(-1).replace('.html', ""))
tab.forEach(item => item !== "" && document.getElementById(item).setAttribute("class", "active"))
}