function rec(element) {
var stack = [],
children = element.children;
for (var i = 0; i <= children.length; i++) {
var child = children[i];
if (child) {
// Тут делаем наше действие
console.log(child);
if (child.children) {
stack.push({
i: i,
children: children
});
children = child.children;
i = -1;
continue;
}
}
if (stack.length) {
var last = stack[stack.length - 1];
children = last.children;
i = last.i;
stack.pop();
}
}
}