-
2016. 2. 5. 02:05 개발자료/W3C
http://codereview.stackexchange.com/questions/28307/implementing-an-algorithm-that-walks-the-dom-without-recursion
Elelement 의경우 아래 코드를 사용하면 됩니다
document.querySelector("*");
Text를 모두 포함하려면 아래 코드를 사용합니다
function iterate(a){
var i=0,r=[];
while (a) {
if (a.nodeType==1
&&a.children.length > 0) {
r.push(a);
a = a.firstChild;
i++;
} else if (a.nextSibling) {
a = a.nextSibling;
r.push(a);
} else {
do {
a = a.parentNode;
i--;
if (!i) {
return r.slice(1);
}
} while (!a.nextSibling)
a = a.nextSibling;
}
}
return r;
}
https://jsfiddle.net/f3ebhg8g/