<
BODY
>
< ul id = " demo " >
< li > a </ li >
< li > b </ li >
< li > c </ li >
</ ul >
< script type = " text/javascript " >
var ul = document.getElementById( ' demo ' );
var liList = ul.getElementsByTagName( ' li ' );
for (var i = 0 ; i < liList.length; ++ i) {
alert(liList.length);
ul.removeChild(liList[i]);
}
</ script >
</ BODY >
< ul id = " demo " >
< li > a </ li >
< li > b </ li >
< li > c </ li >
</ ul >
< script type = " text/javascript " >
var ul = document.getElementById( ' demo ' );
var liList = ul.getElementsByTagName( ' li ' );
for (var i = 0 ; i < liList.length; ++ i) {
alert(liList.length);
ul.removeChild(liList[i]);
}
</ script >
</ BODY >
最后执行的结果是剩下b节点。可以从alert(liList.length);这条语句看出原因在哪里。