How to break out of jQuery each Loop

后端 未结 7 1968
遇见更好的自我
遇见更好的自我 2020-11-22 10:04

How do I break out of a jQuery each loop?

I have tried:

 return false;

In the loop but this did not work. Any ideas?

相关标签:
7条回答
  • 2020-11-22 11:04

    To break a $.each or $(selector).each loop, you have to return false in the loop callback.

    Returning true skips to the next iteration, equivalent to a continue in a normal loop.

    $.each(array, function(key, value) { 
        if(value === "foo") {
            return false; // breaks
        }
    });
    
    // or
    
    $(selector).each(function() {
      if (condition) {
        return false;
      }
    });
    
    0 讨论(0)
提交回复
热议问题