Js native appendChild bug

Source: Internet
Author: User
AppendChild is mainly used to insert a node to the last window. onloadfunction () {varul2document. getElementById (& amp; #39; ul2 & amp; #39;); varolidocument. getElementsByTagName (& amp; #39; li & amp; #39 ;);

AppendChild is mainly used to append a node and insert it to the end.

Window. onload = function (){
Var ul2 = document. getElementById ('ul2 ');
Var oli = document. getElementsByTagName ('lil ');
For (var I = 0; I Ul2.appendChild (oli [I]);
}


}

The content with the lecture Id ul1 is inserted into ul2.


  • 1

  • 2

  • 3

  • 4

  • 5

  • 6



 

 

Insert the content of ul1 into ul2. This is actually a migration task, not a replication task.

 


Why is there such an effect? The reason is that the length is changing due to non-stop moving during the loop. If the for clause is used, the starting length has been fixed, so there is a problem.

 


While (oli. length ){

Ul2.appendChild (oli [0]);


}

This is all about inserting the first entry, so it will be successful. You can try it.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.