Created
December 18, 2014 14:25
-
-
Save huanggm/a39886d1ccb353112fe9 to your computer and use it in GitHub Desktop.
计算DOM节点的深度
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 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