时间:2021-05-28
复制代码 代码如下:
//灵感来自
//http://mon-parent.html
getLCA:function(nodes){
var hash = {}, i = 0,
attr = "data-find"+(++tick),
length = nodes.length,
node,
parent,
counter = 0,
uuid;
while(node = nodes[i++]){
parent = node;
while(parent){
if(parent.nodeType === 1){
break;
}
uuid = parent.getAttribute(attr);
if(!uuid){
uuid = "_" + (++counter);
parent.setAttribute(attr,uuid);
hash[uuid] = {node:parent,count:1};
}else{
hash[uuid].count ++;
}
parent = parent.parentNode;
}
}
for(var i in hash){
if(hash[i].count === length){
return hash[i].node;
}
}
},
getList : function(nodes,parent){//获取当前元素到最近公共祖先间的所有祖先,包括自己
var list = [];
while(node){
if(node === parent){
break;
}
list.unshift(node);
node = node.parentNode;
}
return list;
},
getLists : function(){
var lists = [], getList = Rage.getList, i=0, node, list;
while(node = nodes[i++]){
list = getList(node,parent);
if(list.length){
lists[ lists.length ] = list;
}
}
return lists;
},
sortList : function(a,b){
var n = Math.min(a.length,b.length),ap,bp;
for(var i=0; i < n; i++){
ap = a[i],bp = b[i]
if(ap !== bp){
while(ap = ap.nextSibling){
if(ap === bp){
return -1
}
}
return 1
}
}
return a.length-b.length;
},
uniqueSort : function(nodes){
var length = nodes.length;
var LCA = Rage.getLCA(nodes);
var lists = Rage.getLists(nodes,LCA);
lists.sort(Rage.sortList);
var list, i = 0, result = [];
while(list = lists[i++]){
result[result.length] list.pop();
}
if(result.length !== length){
result.unshift(LAC);
if(result.length != length){
hasDuplicate = true;
}
}
return result;
}
}
下面是第二版,经过改进,终于比jQuery的那个快上三倍(测试对象为拥有260多个节点的文档)
复制代码 代码如下:
var hasDuplicate = false;
var Rage = {
getList : function(node){
var list = [];
while(node){
if(node.nodeType === 9){
break;
}
list.unshift(node);
node = node.parentNode;
}
return list;
},
getLists : function(nodes){
var lists = [], getList = Rage.getList, i=0, node;
while(node = nodes[i++]){
lists[ lists.length ] = getList(node);
}
return lists;
},
sliceList : function(lists,num){
var result = [], i = 0, list;
while(list = lists[i++]){
list = list.slice(num);
if(list.length){
result[ result.length ] = list;
}
}
return result;
},
sortList : function(a,b){
var n = Math.min(a.length,b.length),ap,bp;
for(var i=0; i < n; i++){
ap = a[i],bp = b[i]
if(ap !== bp){
while(ap = ap.nextSibling){
if(ap === bp){
return -1
}
}
return 1
}
}
return a.length-b.length;
},
uniqueSort : function(nodes){
var length = nodes.length;
var lists = Rage.getLists(nodes);
lists.sort(function(a,b){
return a.length - b.length;
});
var depth = lists[0].length, length = lists.length, parent, cut, ii = 0;
for(var i =0; i < depth; i++){
parent = lists[0][i];
cut = true;
for(var j = 1;j < length; j++){
if(parent !== lists[j][i]){
cut = false;
break;
}
}
if(cut){
ii++
}else{
break;
}
}
var LCA = lists[0][ii-1];
lists = Rage.sliceList(lists,ii);
lists.sort(Rage.sortList);
var list, i = 0, result = [];
while(list = lists[i++]){
result[result.length] = list.pop();
}
if(result.length !== length){
result.unshift(LCA);
if(result.length != length){
hasDuplicate = true;
}
}
return result;
}
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
按照从大到小排序appendChild:假设父级a中已经有子节点b,那么a.appendChild(b)的作用是:1.先将子节点b从父级a中删除;2.再将子节点
javascript数组排序汇总//排序算法window.onload=function(){vararray=[0,1,2,44,4,324,5,65,6,6
JavaScript中的Array对象有自己的排序方法sort(),对数组中的数据项进行排序,但是有时候排序结果不尽如人意,比如vararr=[12,1,2,2
本文实例讲述了JavaScript实现节点的删除与序号重建。分享给大家供大家参考。具体如下:这里演示JavaScript节点的删除与重建方法,删除节点后,会自动
首先要理解堆的含义:要么所有节点都不大于其子孩子节点数据,要么都不小于其子孩子节点数据堆排序的核心思想:就是要满足所有节点都满足上面两点,如何完成,看下面堆排序