๐Ÿšงnode.traverse()

๐Ÿ”ฐ JS โŸฉ browser โŸฉ DOM โŸฉ types โŸฉ Node โŸฉ .traverse()

// depth-first traversal
// ๐Ÿ”ธ node.traverse()
Node.prototype.traverse = function(f){
    // do something on this node
    f(this);
    // and on its child nodes
    for(let child of this.childNodes) {
        child.traverse(f);
    } 
};

๐Ÿ’ˆ็ฏ„ไพ‹๏ผš

document.traverse(elem => console.log(elem.nodeName));

Last updated

Was this helpful?