javascript 节点遍历函数
作者:bea
火狐官网上找到的一组函数,相当于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
火狐官网上找到的一组函数,相当于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 !(/[^
]/.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;
}
有用 | 无用
代码如下:
function is_all_ws(nod) { return !(/[^
]/.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 多浏览器分别判断代码
- js setTimeout实现延迟关闭弹出层
- JS 遮照层实现代码
- js压缩工具 yuicompressor 使用教程
- jQuery 遍历json数组的实现代码
- Javascript load Page,load css,load js实现代码
- Javascript Jquery 遍历Json的实现代码
- jquery+json实现的搜索加分页效果
- EXTJS内使用ACTIVEX控件引起崩溃问题的解决方法
- ExtJS TabPanel beforeremove beforeclose使用说明
- Jquery 常用方法一览表(集合)
- 十个优秀的Ajax/Javascript实例网站收集
- javascript中的107个基础知识收集整理 推荐
- jquery CSS选择器笔记
- Tinymce+jQuery.Validation使用产生的BUG
- javascript delete 使用示例代码
- JavaScript 格式字符串的应用
- js DataSet数据源处理代码
- javascript 变态的节点集合