如果运行代码1: 代码如下: var ul = document.getElementById('demo'); var liList = ul.getElementsByTagName('li'); for (var i = 0; i < = liList.length; i++) { ul.removeChild(liList[i]); }
如果和我一样被欺骗了,那么看看正确的代码就明白原因啦! 代码如下: var ul = document.getElementById('demo'); var liList = ul.getElementsByTagName('li'); var lilength = liList.length; for (var i = 0; i < lilength; i++) { ul.removeChild(liList[0]); }