jquery - how to exit (break function) from each statement?
for example I use following code:
$("img").eac开发者_JS百科h(function(i){
x += $("img:eq(" + i ")").width();
if(some conditional) return;
});
after return I would like to not adding anything to x, how could I achieve it?
From the JQuery doco...
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.
So essentially this means...
return false;
= break;
return true;
or return;
= continue;
You can return false
to cause each
to stop calling the function. However, a regular loop and a break statement might be clearer.
精彩评论