Home >Web Front-end >JS Tutorial >js removeChild blindfold possible errors_javascript skills

js removeChild blindfold possible errors_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:45:23998browse

There is a structure:

Copy code The code is as follows:

  • a

  • b

  • c



If you run code 1:
Copy code The code is as follows:

var ul = document.getElementById('demo');
var liList = ul.getElementsByTagName('li');
for (var i = 0; i < = liList.length; i ) {
ul. removeChild(liList[i]);
}

If you were deceived like me, then you will understand the reason after looking at the correct code!
Copy code The code is as follows:

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]);
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn