Jquery each loop break usage
To implement the break and continue functions, use other methods.
Break ---- return false;
Continue -- use return ture;
Function methodone (){
....
$. Each (array, function (){
If (condition true ){
Return true;
}
});
....
}
When you want to use return true in each to return the function, you only need to let the each continue to execute
The each is not interrupted, so the function cannot return.
Let's take a look at a jquery each instance.
In jquery's each method, a break is added to the callback function according to the condition to jump out of the loop. Finally, I checked the usage document. In the past, if the callback function returns false, we can call up the loop. If we want to continue, we can return non-false.
Var len = 0;
Selectedrows. each (function (index ){
If ($ jquery (this). parents ("tr"). find ("span: last"). text () = "process "){
Alert ("records being processed cannot be deleted ");
Return false; // equivalent to break;
}
Len ++;
});
Instance 2
$. Fn. check = function (mode ){
Var mode = mode | 'on'; // if mode is undefined, use 'on' as default
Return this. each (function (){
Switch (mode ){
Case 'on ':
This. checked = true;
Break;
Case 'off ':
This. checked = false;
Break;
Case 'toggle ':
This. checked =! This. checked;
Break;
}
});
};