开发者

jquery - how to exit (break function) from each statement?

开发者 https://www.devze.com 2023-02-21 17:16 出处:网络
for example I use following code: $(\"img\").eac开发者_JS百科h(function(i){ x += $(\"img:eq(\" + i \")\").width();

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

精彩评论

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