JS native AppendChild's Bug solving experience sharing _javascript tips

Source: Internet
Author: User
AppendChild is mainly used to append nodes to the last
Copy Code code as follows:

Window.onload = function () {
var ul2 = document.getElementById (' ul2 ');
var oli = document.getelementsbytagname (' li ');
for (Var i=0;i<oli.length;i++) {
Ul2.appendchild (Oli[i]);
}
}
<ul id= "UL1" >
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
</ul>
<ul id= "Ul2" >
</ul>

Insert the UL1 content into the UL2, which is actually moving instead of copying this to remember
You can look at the effect
Why this effect is due to the cycle of time because of the continuous moving cause length changes if you use for the start length has been fixed so that there is a problem, so to change
Copy Code code as follows:

while (Oli.length) {
Ul2.appendchild (Oli[0]);
}

It's all about inserting the first one so you can try.

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.