我在数组上有一个 jQuery for-each 循环,想知道是否可以提前离开循环。
$(lines).each(function(i){ // some code if(condition){ // I need something like break; } });
break;
实际上不起作用,这是有原因的。
如果我写一个 for 循环,它看起来像这样(但我不想要那样):
for(i=0; i < lines.length; i++){ // some code if(condition){ break; // leave the loop } };
提前致谢 -马丁
最佳答案
根据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.
关于jquery每次早退,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1857186/