开发者

jquery each leave early

I have a jQuery for-each loop on an array and wonder if it is possible to leave the loop early.

$(lines).each(function(i){  
    // some code  
    if(condition){  
开发者_如何学Go        // I need something like break;  
    }  
});

break; actually doesn't work, for a reason.

If I'd write a for-loop it would look like that (but I don't want that):

for(i=0; i < lines.length; i++){  
    // some code  
    if(condition){  
        break; // leave the loop 
    }  
};

Thanks in advance -Martin


According to the docs:

If you wish to break the each() loop at a particular iteration you can do so by making your 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.


Return boolean false;
(SEE http://docs.jquery.com/Utilities/jQuery.each , fourth paragraph)

$(lines).each(function(i){  
    // some code  
    if(condition){  
        // I need something like break;  
        return false;
    }  
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜