ノードツリーを線形に扱う。
ちょっと都合があって作ったプチ関数。メモしとく。
// ノードツリーを線形に扱う。 // 初期化 var X=new nodeL(DOMNode); // リストを進める X.next();//返値 次ノード/ツリー終了時はfalse // 現在のノード X.C function nodeL(T){ var O = T; this.C = O; this.next = function(){ if(! this.C) return this.C; if(this.C.firstChild) return this.C=this.C.firstChild; if(this.C.nextSibling) return this.C=this.C.nextSibling; if(this.C.parentNode == O) return false; return this.C=this.C.parentNode.nextSibling; } }
使用例
ID=sampleのノードを頂点とするノードツリーを処理する
(頂点である ID=sampleのノード は処理しない)
var X=new nodeL(document.getElementById('sample')); while(X.next()){ if(X.C.nodeType==1)alert(X.C.nodeName); }
(改行のみの)テキストノードを飛ばす、とかオプションを付けた方が便利か?そのうち必要があったらやる。