开发者

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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜