我正在尝试理解下面 Crockford 的 walk_the_dom
函数。这是我的逻辑和我正在使用的 DOM 树。我看不到何时到达 node.nextSibling
。
function walk(node, func) {
func(node);
node = node.firstChild;
while (node) {
walk(node, func);
node = node.nextSibling;
}
};
- func(node) 在给定节点上运行函数。
- node=node.firstChild() 现在等于 document.body 的第一个Child,或者在本例中为 H1
- while(node) 运行一个循环,其中再次运行 walk 函数。
- func(node) 现在在 H1 上运行函数
- node = node.firstChild() 现在等于 H1 的第一个Child,在本例中也称为 #text
- while(node) 运行一个循环,其中再次运行 walk 函数
func(node) 在 #text 上运行函数
node = node.firstChild() --> #text 的第一个Child 是什么?
- 什么时候达到node.nextSibling?
最佳答案
#text 的 firstChild
为 null
或 undefined
,因为没有任何值。 JavaScript 具有所谓的“真实值”。因此 while(null)
与 while(false)
相同。
关于javascript - 了解克罗克福德的 walk_the_dom ("The Good Parts"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27880167/