for 循环不能return false 用break中断
forEach 不能终止循环
$.each 与 $().each() 正常
var list = [1,2,3,4,5,6]
for(var i = 0; i<list.length; i++){
console.log('for + '+ list[i])
if(list[i]>3){
break; // return false 必须要用在函数里面,不然会报错
// for + 1 for + 2 for + 3 for + 4
}
}
list.forEach(function(item){
console.log('forEach + ' + item);
if(item > 3){
return false; // 不能终止循环
// forEach + 1 forEach + 2 forEach + 3 forEach + 4 forEach + 5 forEach + 6
}
})
$.each(list, function(index, item) {
console.log('$.each + ' + item);
if(item > 3){
return false; // $.each + 1 $.each + 2 $.each + 3 $.each + 4
}
})
$(list).each(function(index, item) {
console.log('$().each + ' + item);
if(item > 3){
return false; // $().each + 1 $().each + 2 $().each + 3 $().each + 4
}
})