Break $.each loop

結束 jQuery.each 的循環是通過設定 jQuery.each 的 callback 函數中返回 false
而繼續下一個循環(即 continue ),是 return 非 false 值。亦適用於 .each

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
(function(){
// 結束循環(即break)
jQuery.each(aOptionList, function(index, option){
if (option.innerHTML === "stop") {
return false; // 返回false即break掉each循環
}
// magic
});
// 繼續下一個循環(即continue)
jQuery.each(aOptionList, function(index, option){
if (option.innerHTML === "stop") {
return "continue"; // 返回任意非false值
}
// magic
});
}());

官方說明:

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.

参考资料:

0%