Created
December 13, 2011 19:34
All the DOM recursion you'll ever need
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var slice = Array.prototype.slice | |
function iterativelyWalk(nodes, cb) { | |
nodes = slice.call(nodes) | |
while(nodes.length) { | |
var node = nodes.shift(), | |
ret = cb(node) | |
if (ret) { | |
return ret | |
} | |
if (node.childNodes.length) { | |
nodes = slice.call(node.childNodes).concat(nodes) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function recursivelyWalk(nodes, cb) { | |
for (var i = 0, len = nodes.length; i < len; i++) { | |
var node = nodes[i], | |
ret = cb(node) | |
if (ret) { | |
return ret | |
} | |
if (node.childNodes.length) { | |
var ret = recursivelyWalk(node.childNodes, cb) | |
if (ret) { | |
return ret | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment