Javascript/JQuery:通过匿名函数在调用each()时从PARENT函数立即返回

Gre*_*nie 1 javascript jquery

例如:

> function foo() {
>    jQuery(whatever).each( function() {
         return; // this just exits the anonymous function - is there a way to return from foo?
     }
   );
> 
> }
Run Code Online (Sandbox Code Playgroud)

Lee*_*Lee 11

**更正:添加更多细节.使用标志允许从PARENT函数返回**

function foo() {
   var doreturn = false;
   jQuery(whatever).each( function() {
     if(youwanttoreturn){
         doreturn=1;
         return false;
     }
   });
   if(doreturn)return;
}
Run Code Online (Sandbox Code Playgroud)

http://api.jquery.com/each/ "我们可以通过返回false来停止回调函数中的循环."