Created
July 17, 2013 06:52
-
-
Save lizzie/6018208 to your computer and use it in GitHub Desktop.
获取节点公共的父节点
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 getCommonAncestor(nodes) { | |
if (!nodes || !nodes.length) { | |
return null; | |
} | |
else if (nodes.length == 1) { | |
return nodes[0].parentNode; | |
} else { | |
var p = nodes[0], | |
pass = 0; | |
while (!pass && p != document.body) { | |
p = p.parentNode; | |
pass = 1; | |
for (var i = 1; i < nodes.length; i++) { | |
if (!DOM.contains(p, nodes[i])) { | |
pass = 0; | |
break; | |
} | |
} | |
} | |
return p; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment