Home > Web Front-end > JS Tutorial > body text

JQuery跳出each循环的方法_jquery

WBOY
Release: 2016-05-16 16:03:43
Original
950 people have browsed it

一、jquery each循环,要实现break和continue的功能:

break----用return false;
continue --用return ture;

二、jquery怎么跳出当前的each循环

有些朋友可能会以为在jquery跳出循环可以直接使用continue和break了,但是使用之后没有效果,因为在jquery中没有这两条命令。

后来上网查了下,得到了结果:
return false;——跳出所有循环;相当于 javascript 中的 break 效果。
return true;——跳出当前循环,进入下一个循环;相当于 javascript 中的 continue 效果

复制代码 代码如下:

$(function (){
 $("input[type='text']").each(function (i){ 
  var _val=$(this).val();
  alert(_val);
  if(_val=='2'){  
   return false; //跳出循环
  }
 })
});

三、Jquery each方法跳出循环并获得返回值的方法

return false:将停止循环 (就像在普通的循环中使用 'break')。
return true:跳至下一个循环(就像在普通的循环中使用'continue')。

复制代码 代码如下:

function test(){
var success = false;
$(..).each(function () {
   if (..) {
       success = true;
       return false;
   }
});
 return success ;
}

jquery是对象链,所以$(..).each()返回的还是对象集合。each(function(){}):是回调函数,在回调函数里不能返回结果到回调函数each外面。
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!