开发者

Quit jQuery $.each loop [duplicate]

开发者 https://www.devze.com 2023-01-31 17:42 出处:网络
This question already has answers here: Closed 12开发者_如何学JAVA years ago. Possible Duplicate:
This question already has answers here: Closed 12开发者_如何学JAVA years ago.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消