为什么必须要使用getElementById("table").lastChild
为什么必须要使用getElementById("table").lastChild,这里使用lastChild的结果为什么不是table的最后一个tr呢?
为什么必须要使用getElementById("table").lastChild,这里使用lastChild的结果为什么不是table的最后一个tr呢?
2016-10-05
我是这样测试的,现实的结果很明白。
window.onload = function(){
var ta=document.getElementById("table").childNodes;
// alert(ta.length) //提示为2.
document.write(
"ta[0].nodeType:" + ta[0].nodeType+
"<br/> ta[0].nodeName" + ta[0].nodeName +
"<br/> ta[1].nodeType:" +ta[1].nodeType +
"<br/> ta[1].nodeName:"+ ta[1].nodeName
);
// alert(ta[1].childNodes.length) //提示为6,也就是我们以为的table里的子节点数目,实际是在
//table的childNodes[1].childNodes里
}ta[0].nodeType:3
ta[0].nodeName#text
ta[1].nodeType:1
ta[1].nodeName:TBODY
这下就明白为什么要是使用 document.getElementById("table").lastChild 了,希望可以帮到你。
举报