Skip to content

Instantly share code, notes, and snippets.

@huanggm
Created December 18, 2014 14:25
Show Gist options
  • Save huanggm/a39886d1ccb353112fe9 to your computer and use it in GitHub Desktop.
Save huanggm/a39886d1ccb353112fe9 to your computer and use it in GitHub Desktop.
计算DOM节点的深度
function cal(node, cur) {
cur = cur || 1;
var c = node.childNodes;
if(node.nodeName === "IFRAME") {
c = [node.contentDocument];
}
var i = 0, l = c.length, maxarr=[];
if(c && l) {
cur ++;
while(i < l) {
maxarr[i] = cal(c[i], cur);
i ++;
}
return Math.max.apply(null, maxarr);
}
return cur;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment