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;