原书中说:IE中nextSibling比childNodes表现优异。在IE6中,nextSibling快16倍,IE7中是105倍。
经测试后发现:IE7下:nextSibling要快一些,childNodes与childnren速度相当。IE6下:nextSibling与childNodes差别相当大,children稍快。FF下还是nextSibling快一点。
function testNextSibling(){
var el = $('mydiv'),
ch = el.firstChild,
name = '';
do {
name = ch.nodeName;
} while (ch = ch.nextSibling);
return name;
}
function testChildNodes(){
var el = $('mydiv'),
ch = el.childNodes,
len = ch.length,
name = '';
for(var count=0; count<len; count++){
name = ch[count].nodeName;
}
return name;
}
function testChildren(){
var el = $('mydiv'),
ch = el.children,
len = ch.length,
name = '';
for(var count=0; count<len; count++){
name = ch[count].nodeName;
}
return name;
}
var el = $('mydiv'),
ch = el.firstChild,
name = '';
do {
name = ch.nodeName;
} while (ch = ch.nextSibling);
return name;
}
function testChildNodes(){
var el = $('mydiv'),
ch = el.childNodes,
len = ch.length,
name = '';
for(var count=0; count<len; count++){
name = ch[count].nodeName;
}
return name;
}
function testChildren(){
var el = $('mydiv'),
ch = el.children,
len = ch.length,
name = '';
for(var count=0; count<len; count++){
name = ch[count].nodeName;
}
return name;
}
正美: