How to break/exit from a each() function in JQuery?

This question already has an answer here:

  • How to break out of jQuery each Loop 5 answers

  • According to the documentation you can simply return false; to break:

    $(xml).find("strengths").each(function() {
    
        if (iWantToBreak)
            return false;
    });
    

    Return false from the anonymous function:

    $(xml).find("strengths").each(function() {
      // Code
      // To escape from this block based on a condition:
      if (something) return false;
    });
    

    From the documentation of the each method:

    Returning 'false' from within the each function completely stops the loop through all of the elements (this is like using a 'break' with a normal loop). Returning 'true' from within the loop skips to the next iteration (this is like using a 'continue' with a normal loop).


    你可以使用return false;

    +----------------------------------------+
    | JavaScript              | PHP          |
    +-------------------------+--------------+
    |                         |              |
    | return false;           | break;       |
    |                         |              |
    | return true; or return; | continue;    |
    +-------------------------+--------------+
    
    链接地址: http://www.djcxy.com/p/80964.html

    上一篇: 函数不能被调用

    下一篇: 如何在JQuery中打破/退出each()函数?