火狐官网上找到的一组函数,相当于treeWalker,有了它可以方便地在IE实现Traversal API 2的所有功能(nextElementSibling,previousElementSibling,firstElementChild,lastElementChild,children)These functions let you find the next sibling, previous sibling, first child, and last child of a given node (element). What makes them unique is that they safely ignore whitespace nodes so you get the real node you're looking for each time. 复制代码 代码如下:function is_all_ws(nod) { return !(/[^tnr ]/.test(nod.data)); } function is_ignorable(nod) { return (nod.nodeType == 8) || ((nod.nodeType == 3) && is_all_ws(nod)); } function node_before(sib) { while ((sib = sib.previousSibling)) { if (!is_ignorable(sib)) return sib; } return null; } function node_after(sib) { while ((sib = sib.nextSibling)) { if (!is_ignorable(sib)) return sib; } return null; } function first_child(par) { var res = par.firstChild; while(res) { if(!is_ignorable(res)) return res; res = res.nextSibling; } return null; } function last_child(par) { var res = par.lastChild; while(res) { if(!is_ignorable(res)) return res; res = res.previousSibling; } return null; }
推荐阅读
js本身的局限性 别让javascript做太多事
甚至有个人问我“页面上有5个文本框,每个文本框只能输入一个字符,怎么用js实现用户输入第一个文本框的后焦点自动跳到第二个。”。 我说这个功能不是应该是Tab做的吗?用户输入后只要按一下Tab键就可以了,而且,>>>详细阅读
本文标题:javascript 节点遍历函数
地址:http://www.17bianji.com/kaifa2/JS/26786.html
1/2 1