一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

jQuery 跳出each循环方法详解

时间:2013-11-04 编辑:简简单单 来源:一聚教程网

jQuery 跳出each循环不能使用break和continue,要使用return,

break----用return false;

continue --用return ture;

 

 代码如下 复制代码
$.each(array,function()...{
   if(条件1成立)...{
     return true; //相当于continue;
   }
   if(条件2成立)...{
     return false; //相当于break;
   }
});

具体如下

例1

//跳出整个each循环,break 功能

 代码如下 复制代码
$(".test").each(function(i){
   
    if(i==3){
       return false;
    }
    alert(i);
});

例2.

跳出本次循环,进入下次循环,continue 功能

 代码如下 复制代码
$(".test").each(function(i){
   
    if(i==3){
       return true;
    }
    alert(i);
});

答案是使用 return false;  切记哦,不是使用break;也不是直接使用return;

热门栏目