jQuery中each的用法之退出循环和结束本次循环
jQuery中each类似于javascript的for循环
但不同于for循环的是在each里面不能使用break结束循环,也不能使用continue来结束本次循环,想要实现类似的功能就只能用return,
break 用return false
continue 用return ture
列如:
// 全选
$("#ID").click(function() {
var $checkedInputs = $(chekcDivId +" input");
$checkedInputs.each(function(){
var thiz = $(this);
var mid = thiz.val();
if(checkedMids.indexOf(mid) != -1){
return true;// 相当于 java中的 continue
}
if(checkedMids.length == 20){
return false;// 相当于 java中的 break
}
checkedMids.push(mid);
// thiz.attr('checked', true);
// 添加标签固有属性的时候用prop,非固有属性用attr
thiz.prop("checked",true);
});
});

浙公网安备 33010602011771号