繁体   English   中英

如何在jquery中突破$ .each?

[英]How do I break out of an $.each in jquery?

当条件满足时,如何在每个方法中突破以下jquery;

var rainbow = {'first' : 'red', 'second' : 'orange', 'third' : 'yellow'};

$.each(rainbow, function (key, color) {

  if (color == 'red') {

    //do something and then break out of the each method
    alert("i'm read, now break.");

  }

});
var rainbow = {'first' : 'red', 'second' : 'orange', 'third' : 'yellow'};

$.each(rainbow, function (key, color) {

  if (color == 'red') {

    //do something and then break out of the each method
    alert("i'm read, now break.");

    return false;

  }

});

正如在$.each jQuery页面上明确写的$.each

我们可以通过使回调函数返回false来在特定迭代中中断$ .each()循环。 返回非false与for循环中的continue语句相同; 它将立即跳到下一次迭代。

在发布此处之前,请先搜索您搜索的字词! 很遗憾,如果你不费心阅读它,jQuery有史以来最好的文档之一!

每种状态的JQuery文档

我们可以通过使回调函数返回false来在特定迭代中中断$ .each()循环。 返回非false与for循环中的continue语句相同; 它将立即跳到下一次迭代。

它还提供了示例。

<script>
    var arr = [ "one", "two", "three", "four", "five" ];

    jQuery.each(arr, function() {
      $("#" + this).text("Mine is " + this + ".");
       return (this != "three"); // will stop running after "three"
   });


</script>

尝试这个

http://api.jquery.com/jQuery.each/

我们不能在jquery函数中使用Break和Continue
尝试这个

var rainbow = {'first' : 'red', 'second' : 'orange', 'third' : 'yellow'};

$.each(rainbow, function (key, color) {

  if (color == 'red') {
    //do something and then break out of the each method
    alert("i'm read, now break.");
    return false;
  }
  alert(color);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM