Skip to content

Instantly share code, notes, and snippets.

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 idiotWu/efedfe5c6153ce5740662b841c0d0849 to your computer and use it in GitHub Desktop.
Save idiotWu/efedfe5c6153ce5740662b841c0d0849 to your computer and use it in GitHub Desktop.
取两个HTML节点最近的公共父节点
function getCommonParent(el1,el2){
var parents1 = [];
var el = el1;
while(el) {
parents1.unshift(el);
el = el.parentNode;
}
var parents2 = [];
var el = el2;
while(el) {
parents2.unshift(el);
el = el.parentNode;
}
var i = 0;
while(i<parents1.length && i<parents2.length && parents1[i+1] == parents2[i+1])
i++;
return parents1[i];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment