-
-
Notifications
You must be signed in to change notification settings - Fork 99
/
dom.js
46 lines (43 loc) · 1.07 KB
/
dom.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
/*
* Author : OBKoro1
* Date : 2021-09-17 12:24:17
* LastEditors : OBKoro1
* LastEditTime : 2021-11-04 16:07:51
* FilePath : /js-base/src/scene/dom.js
* description : 如何遍历一个dom树
* koroFileheader VSCode插件
* Copyright (c) 2021 by OBKoro1, All Rights Reserved.
*/
// 递归写法
function traversal(node) {
// 对node的处理
if (node && node.nodeType === 1) {
console.log(node.tagName)
}
// 递归先序遍历子节点
let { childNodes } = node
let item
for (let i = 0; i < childNodes.length; i++) {
item = childNodes[i]
if (item.nodeType === 1) {
traversal(item)
}
}
}
// 广度遍历 栈写法
function traversal2(node) {
const stack = []
stack.push(node)
while (stack.length > 0) {
const elem = stack.pop()
if (elem && elem.nodeType === 1) {
console.log(elem.tagName)
const { children } = elem
const len = children.length
// 子节点入栈 添加到头部 先进先出
for (let i = 0; i < len; i++) {
stack.unshift(children[i])
}
}
}
}