Detailed explanation of jQuery's method for jumping out of the each loop

Source: Internet
Author: User

In jquery, if you still use break and continue in php to jump out of the loop, you will be wrong. In jquery, you only need to use return to jump out of the loop or enter the next loop.

The break and continue cannot be used for jQuery to exit the each loop. return must be used,

Break ---- return false;

Continue -- use return ture;

 

The Code is as follows: Copy code
$. Each (array, function ()...{
If (condition 1 is true )...{
Return true; // equivalent to continue;
}
If (condition 2 is true )...{
Return false; // equivalent to break;
}
});

The details are as follows:

Example 1

// Jump out of the entire each loop and use the break Function

The Code is as follows: Copy code
$ (". Test"). each (function (I ){

If (I = 3 ){
Return false;
}
Alert (I );
});

Example 2.

Jump out of this loop and enter the next loop. The continue Function

The Code is as follows: Copy code
$ (". Test"). each (function (I ){

If (I = 3 ){
Return true;
}
Alert (I );
});

The answer is to use return false. Remember not to use break or directly use return;

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.