-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtree.html
59 lines (54 loc) · 1.17 KB
/
tree.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<!-- 树的遍历 -->
<body>
<div id="app">
<div>哈喽</div>
<p>开课吧</p>
<div>
<span>123</span>
<button>xx</button>
</div>
<ul>
<li>123</li>
</ul>
</div>
<script>
function walk(node,func=()=>{}){
if(node instanceof window.Node){
_walk(node, func)
}
return node
}
function _walk(node, func){
func(node)
// 需要递归的
// 获取第一个子元素
node = node.firstChild
// 只要子元素存在
while(node){
// 对这个子元素 递归遍历
_walk(node,func)
// 遍历结束,开始遍历他的兄弟节点
node = node.nextSibling
}
}
walk(document.getElementById('app'), node=>{
console.log(node)
})
// {
// type:"div",
// props:{
// children:[]
// }
// }
// 根据props.children来遍历
</script>
</body>
</html>