作家
登录

javascript 节点遍历函数

作者: 来源:www.28hudong.com 2013-03-30 01:45:02 阅读 我要评论

火狐官网上找到的一组函数,相当于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

关键词: 探索发现

乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

网友点评
自媒体专栏

评论

热度

精彩导读
栏目ID=71的表不存在(操作类型=0)