ITPub博客

首页 > IT职业 > IT职场 > jQuery跳出循环的方法

jQuery跳出循环的方法

IT职场 作者:Hooman 时间:2021-09-09 09:37:17 0 删除 编辑

对于each循环

jQuery中each类似于javascript的for循环
但不同于for循环的是在each里面不能使用break结束循环,也不能使用continue来结束本次循环,想要实现类似的功能就只能用return,对应关系如下:
break           用return false
continue      用return ture

举例如下:

//用于循环同型元素
  $("[test-div]").each(function () {       var id = $(this).attr("id").toString();       if(id=='2'){          return true;
       }       if(id == '10'){          return false;
       }
  })//用于循环json
 var json = [
     {"id":"1","ownValue":"11"},
     {"id":"2","ownValue":"22"},
     {"id":"3","ownValue":"33"},
     {"id":"4","ownValue":"44"},
     {"id":"5","ownValue":"55"} ]
$.each(json, function(idx, obj) {    if(obj.id%2==0)return true;    if(obj.id==7)return false;    console.log(obj.ownValue);
});

对于for循环

for 循环一般用于遍历某个值,用 continue 和 break 关键字跳出循环

举例如下

 for(var i=0;i



作者:weberweber
链接:https://www.jianshu.com/p/ae86f2c11927

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1273/viewspace-2810988/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-15

  • 博文量
    120
  • 访问量
    1201