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