Possible Duplicate:
How do I break out of an $.each in jquery?
How to quit the jquery $.each loop?
Use return false
inside the .each()
loop to break out entirely. Returning anything that's not false is like continue
: it stops the current iteration and jumps right to the next.
var myArr = [1,2,3,4,5,6,7];
$.each( myArr, function(){
// Skip on three
if( this === 3 ) return true;
// Abort on five
if( this === 5 ) return false;
doStuff( this ); // never for 3, 5, 6 or 7
});
http://api.jquery.com/jQuery.each/
We can break the $.each() loop at a particular iteration by making the callback function return false. Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration.
continue
and break
do not work the same since you are passing a callback and jQuery does the loop, but you can emulate them:
To continue
, return true
inside the .each callback.
To break
, return false
.
精彩评论