For and while problems in JavaScript

Source: Internet
Author: User

There are so many things that I can't understand in JavaScript, some problems I know how to modify, but I can't explain why. Recently, another one has been encountered.

A "Select All-delete" feature, the following is the following is the click "Delete" button out of the trigger code fragment: var tbody = document.getElementById ("MAPPINGTD");
var Mappingcheck = document.getelementsbyname ("checkboxinmapping");

var length = Mappingcheck.length;
for (i = 0; < length; ) {
if (Mappingcheck.item (i). Checked) {
var str_value = Mappingcheck.item (i). value;
var tr = document.getElementById (str_value);
Tbody.removechild (TR);
}
else {
i++;
}
}

This code sometimes "select all" and can not delete all the selected information, the probability is about 2/3. My first reaction was to change the for to while, and in the past encountered similar problems, for and while in JS sometimes not interchangeable. The following is the modified code: var tbody = document.getElementById ("MAPPINGTD");
var Mappingcheck = document.getelementsbyname ("checkboxinmapping");

var length = Mappingcheck.length;
var i = 0;
while (I < length) {
if (Mappingcheck.item (i). Checked) {
var str_value = Mappingcheck.item (i). value;
var tr = document.getElementById (str_value);
Tbody.removechild (TR);
}
else {
i++;
}
}

This time can complete the required functions, two pieces of code should be interchangeable, do not know why this problem, I know it, I do not know why.

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.