Home > Web Front-end > JS Tutorial > Example code sharing of JQuery jumping out of each loop

Example code sharing of JQuery jumping out of each loop

零下一度
Release: 2017-06-28 14:42:12
Original
1252 people have browsed it

This article mainly introduces how JQuery jumps out of the each loop. This article explains how jquery jumps out of the current each loop, how Jquery each method jumps out of the loop and gets the return value, etc. Friends who need it can refer to it

1. jquery each loop needs to realize the functions of break and continue:

break----use return false;
continue --use return ture;

2. How to jump out of the current each loop in jquery

Some friends may think that you can directly use continue and break to jump out of the loop in jquery, but it has no effect after using it, because there are no these two commands in jquery.

Later I checked online and got the result:
return false;——Jump out of all loops; equivalent to the break effect in javascript.
return true;——Jump out of the current loop and enter the next loop; equivalent to the continue effect in javascript
Example

$(function (){
$("input[type= 'text']").each(function (i){
var _val=$(this).val();
alert(_val);
if(_val=='2'){
return false; //Jump out of the loop
}
})
});


3. Jquery each method jumps out of the loop And get the return value of the method

return false: will stop the loop (just like using 'break' in a normal loop).
return true: jump to the next loop (just like using 'continue' in a normal loop).

function test(){
var success = false;
$(..).each(function () {
if (..) {
success = true;
         return false;
                                             ); each() still returns a collection of objects. each(function(){}): It is a callback function. In the callback function, the result cannot be returned outside the callback function each.



The above is the detailed content of Example code sharing of JQuery jumping out of each loop. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template