function walk(nodes, cb) {
var cursor = nodes[0], last, ret;
do {
last = cursor;
do {
if (ret = cb(cursor)) return ret;
} while (cursor = cursor.firstChild);
cursor = last;
} while (cursor = cursor.nextSibling);
}
walk(document.body.children, function(x){console.log(x)})