Skip to content

Instantly share code, notes, and snippets.

@lizzie
Created July 17, 2013 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lizzie/6018208 to your computer and use it in GitHub Desktop.
Save lizzie/6018208 to your computer and use it in GitHub Desktop.
获取节点公共的父节点
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