[英]How do I break out of an $.each in jquery?
How to I break out of the following jquery each method when a condition is met; 当条件满足时,如何在每个方法中突破以下jquery;
var rainbow = {'first' : 'red', 'second' : 'orange', 'third' : 'yellow'};
$.each(rainbow, function (key, color) {
if (color == 'red') {
//do something and then break out of the each method
alert("i'm read, now break.");
}
});
We can break the $.each() loop at a particular iteration by making the callback function return false. 我们可以通过使回调函数返回false来在特定迭代中中断$ .each()循环。 Returning non-false is the same as a continue statement in a for loop;
返回非false与for循环中的continue语句相同; it will skip immediately to the next iteration.
它将立即跳到下一次迭代。
var rainbow = {'first' : 'red', 'second' : 'orange', 'third' : 'yellow'};
$.each(rainbow, function (key, color) {
if (color == 'red') {
//do something and then break out of the each method
alert("i'm read, now break.");
return false;
}
});
As explicitly written on jQuery's page for $.each
: 正如在
$.each
jQuery页面上明确写的$.each
:
We can break the $.each() loop at a particular iteration by making the callback function return false.
我们可以通过使回调函数返回false来在特定迭代中中断$ .each()循环。 Returning non-false is the same as a continue statement in a for loop;
返回非false与for循环中的continue语句相同; it will skip immediately to the next iteration.
它将立即跳到下一次迭代。
Please google the terms you search before posting here ! 在发布此处之前,请先搜索您搜索的字词! It's a pity that jQuery has one of the best documentations ever if you do not bother to read about it !
很遗憾,如果你不费心阅读它,jQuery有史以来最好的文档之一!
The JQuery documentation for each states: 每种状态的JQuery文档 :
We can break the $.each() loop at a particular iteration by making the callback function return false.
我们可以通过使回调函数返回false来在特定迭代中中断$ .each()循环。 Returning non-false is the same as a continue statement in a for loop;
返回非false与for循环中的continue语句相同; it will skip immediately to the next iteration.
它将立即跳到下一次迭代。
It also provides examples. 它还提供了示例。
<script>
var arr = [ "one", "two", "three", "four", "five" ];
jQuery.each(arr, function() {
$("#" + this).text("Mine is " + this + ".");
return (this != "three"); // will stop running after "three"
});
</script>
Try this 尝试这个
http://api.jquery.com/jQuery.each/ http://api.jquery.com/jQuery.each/
We can not use Break and Continue in jquery functions 我们不能在jquery函数中使用Break和Continue
Try this 尝试这个
var rainbow = {'first' : 'red', 'second' : 'orange', 'third' : 'yellow'};
$.each(rainbow, function (key, color) {
if (color == 'red') {
//do something and then break out of the each method
alert("i'm read, now break.");
return false;
}
alert(color);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.