🚧node.traverse()

🔰 JSbrowserDOMtypesNode ⟩ .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