jquery jump out of each loop example

Source: Internet
Author: User

Example

The code is as follows Copy Code

<script type= "Text/javascript" >
$ (document). Ready (function () {
$ ("button"). Click (function () {
$ ("Li"). each (function () {
Alert ($ (this). Text ())

});
});
});
</script>

<ul>
<li>Coffee</li>
<li>Milk</li>
<li>Soda</li>
</ul>

can achieve output


Coffee
Milk
Soda

But we

The code is as follows Copy Code

<script type= "Text/javascript" >
$ (document). Ready (function () {
$ ("button"). Click (function () {
$ ("Li"). each (function () {
Alert ($ (this). Text ())
Break ;
    });
});
});
</script>

Then found that there is a JS error, when I changed back again can, so feel break; in jquery, the invalid, Baidu issued the following instructions.

To implement the function of break and continue, in jquery $.each the corresponding use of return true and return false. :

The code is as follows Copy Code

$ ("div"). each (function (index, Element) {
Break
Which is to jump out of the entire each loop
return false;
});

$ ("div"). each (function (index, Element) {
Continue
That is, jump out of each loop and execute the next loop
return true;
});

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.