Created
August 25, 2014 17:43
-
-
Save crimx/9ecb82ddfc549817c63c to your computer and use it in GitHub Desktop.
编写一个JavasSript函数,给定一个DOM节点node和一个正整数n, 返回node的所有第n代后代节点(直接子节点为第1代)
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 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