javascript 節(jié)點遍歷函數(shù)
更新時間:2010年03月28日 14:02:32 作者:
火狐官網(wǎng)上找到的一組函數(shù),相當于treeWalker,有了它可以方便地在IE實現(xiàn)Traversal API 2的所有功能
火狐官網(wǎng)上找到的一組函數(shù),相當于treeWalker,有了它可以方便地在IE實現(xiàn)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 !(/[^\t\n\r ]/.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;
}
復(fù)制代碼 代碼如下:
function is_all_ws(nod) { return !(/[^\t\n\r ]/.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 使用for循環(huán)遍歷子節(jié)點查找元素
- js查找父節(jié)點的簡單方法
- javascript下查找父節(jié)點的簡單方法
- js查找節(jié)點的方法小結(jié)
- JS常見DOM節(jié)點操作示例【創(chuàng)建 ,插入,刪除,復(fù)制,查找】
- javascript基礎(chǔ)之查找元素的詳細介紹(訪問節(jié)點)
- js遍歷子節(jié)點子元素附屬性及方法
- JS獲取子節(jié)點、父節(jié)點和兄弟節(jié)點的方法實例總結(jié)
- javascript獲取網(wǎng)頁中指定節(jié)點的父節(jié)點、子節(jié)點的方法小結(jié)
- javascript得到XML某節(jié)點的子節(jié)點個數(shù)的腳本
- JS查找孩子節(jié)點簡單示例
相關(guān)文章
JS實現(xiàn)圖片轉(zhuǎn)換成base64的各種應(yīng)用場景實例分析
這篇文章主要介紹了JS實現(xiàn)圖片轉(zhuǎn)換成base64的各種應(yīng)用場景,結(jié)合實例形式分析了javascript實現(xiàn)圖片轉(zhuǎn)換成base64的各種應(yīng)用場景的相關(guān)操作技巧與使用注意事項,需要的朋友可以參考下2018-06-06ExtJs的Ext.Ajax.request實現(xiàn)waitMsg等待提示效果
這篇文章主要介紹了ExtJs的Ext.Ajax.request實現(xiàn)waitMsg等待提示效果,需要的朋友可以參考下2017-06-06檢查輸入的是否是數(shù)字使用keyCode配合onkeypress事件
檢查輸入的是否是數(shù)字在本文使用keyCode配合onkeypress事件來實現(xiàn),具體示例如下2014-01-01