-
$.each(array, function(index, a) {
-
if (met condition) {
-
return someValue;
-
}
-
});
- return otherValue;
查看了一些資料才發現,原來在jQuery.each中使用 return false 相當於在for或while循環中使用break,return true則相當於使用continue,這不禁想起了js的事件冒泡。
其實可以這樣來理解,jQuery.each相當於其中function的父事件,在function中return false則阻止jQuery.each運行,也就相當於使用break,這也就很容易理解爲什麼return true就相當於使用continue了。
如此一來,上述程序可以寫成如下形式
-
var ret = otherValue;
-
$.each(array, function(index, a) {
-
if (met condition) {
-
ret = someValue;
-
return false; // break;
-
}
-
});
- return ret;