手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >javascript 节点遍历函数
javascript 节点遍历函数
摘要:火狐官网上找到的一组函数,相当于treeWalker,有了它可以方便地在IE实现TraversalAPI2的所有功能(nextElement...

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

}

【javascript 节点遍历函数】相关文章:

javascript转换静态图片,增加粒子动画效果

JavaScript中指定函数名称的相关方法

javaScript中with函数用法实例分析

深入浅析JavaScript面向对象和原型函数

javascript中this的四种用法

javascript函数特点实例

javascript实现Table排序的方法

用JavaScript实现页面重定向功能的教程

javascript事件冒泡实例分析

javascript无刷新评论实现方法

精品推荐
分类导航