Created
April 9, 2013 03:55
-
-
Save wintercn/5342839 to your computer and use it in GitHub Desktop.
取两个HTML节点最近的公共父节点
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 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]; | |
} |
function getCommonParent(el1,el2) {
if (!el1 || !el2)
return null;
var commonParent = null;
var nodes = [el1, el2];
for (var i=0; i<nodes.length; ++i) {
var node = nodes[i];
if (node.gcpVisited) {
commonParent = node;
break;
}
node.gcpVisited = true;
if (node = node.parentNode) {
nodes.push(node);
}
}
while (node = nodes.pop()) {
delete node.gcpVisited;
}
return commonParent;
}
两边一起来,如果离共同父类距离相近时则查询次数较少,但有设flag和清flag消耗,综合实力就不清楚了。
我也来一段吧
function getCommonParent(a,b){
if(a.sourceIndex){ //for IE
var sib = b.sourceIndex, sia = a.sourceIndex;
if(sib < sia){
return getCommonParent(b,a);
}
while((sib > sia) && (b = b.parentNode)){
sib = b.sourceIndex;
}
return b && b.contains(a) ? b : null;
}else{ //Not IE
//...参考chaoren1641
}
}
@cuixiping 真是各种高人啊...... 长见识了
再来一段使用的标准compareDocumentPosition的,把代码修改得简洁了一点。
function getCommonParent(a,b){
var c = a.compareDocumentPosition(b);
if(c & 8){
return b.parentNode;
}else if(c & 16){
return a.parentNode;
}else if(c & 6){
while(!(8 & a.compareDocumentPosition(b=b.parentNode)));
return b;
}
return null;
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@wintercn 不应该使用unshift方法,应该使用push方法,相应的后面也就要从尾部开始了
@coffeesherk 实现很好,空间复杂度明显要小,++运算应该很快哦,做一点无关痛痒的修改