Home >Web Front-end >JS Tutorial >Sharing experience in solving bugs in js native appendChild_javascript skills

Sharing experience in solving bugs in js native appendChild_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:30:381376browse

appendChild is mainly used to append nodes to the end

Copy code The code is as follows:

window. onload = function(){
var ul2 = document.getElementById('ul2');
var oli = document.getElementsByTagName('li');
for(var i=0;iul2.appendChild(oli[i]);
}
}

Insert the content with Id ul1 into ul2



  • 1

  • 2

  • 3

  • 4

  • 5

  • 6





Insert the content of ul1 into ul2. This is actually moving rather than copying. Remember
You can check the effect
Why This effect occurs because the length is changed due to constant moving during the loop. If you use for, the starting length has been fixed, so there is a problem, so it needs to be changed to
Copy code The code is as follows:

while(oli.length){
ul2.appendChild(oli[0]);
}

This is because the first one is inserted, so it is successful. You can try it.
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