Skip to content

Instantly share code, notes, and snippets.

@crimx
Created August 25, 2014 17:43
Show Gist options
  • Save crimx/9ecb82ddfc549817c63c to your computer and use it in GitHub Desktop.
Save crimx/9ecb82ddfc549817c63c to your computer and use it in GitHub Desktop.
编写一个JavasSript函数,给定一个DOM节点node和一个正整数n, 返回node的所有第n代后代节点(直接子节点为第1代)
function getDescendants(node, n) {
var res = [];
function f(children, lev) {
if (lev >= n) {
res = res.concat(Array.prototype.slice.apply(children));
return;
}
for (var i = 0; i < children.length; i += 1) {
f(children[i].children, lev+1);
}
}
f(node.children, 1);
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment