function myremove() { var mytable=document.getElementById("table"); var mytr=document.getElementsByTagName("tr"); for (var i=0;i<mytr.length;i++){ if (i!=0){ mytable.removeChild(mytr[i]); } } }
table下面只有text和tbody,tr是在tbody里面的。也可以写成mytable.childNodes[1].removeChild(mytr[i]);
function myremove() { var mytable=document.getElementById("table"); var mytr=document.getElementsByTagName("tr"); for (var i=0;i<mytr.length;i++){ if(i!=0){ //改成这样就行了: mytr[i].parentNode.removeChild(mytr[i]); } } }