想象一下,我想遍历一个 jQuery 对象列表,并为每个对象执行一些函数。但是,如果在某些地方,不满足条件,那么我只是想 continue;
到其他对象。简单的伪代码可能是这样的:
$('#element').each(function(){
// some code here
if (!$(this).is('.included')) {
continue; // This keyword of course, doesn't work here
}
// more code here, which won't get executed for some elements
});
我想达到同样的效果:
for (var i = 0; i < 10; i++) {
if (i % 2 == 0 ) {
continue;
}
console.log(i);
}
我知道我可以在 each();
方法中的某处 return false;
来完全打破循环。但我不想打破循环。我只想跳过一些元素。我也知道我可以使用 if-else
block 来跳过元素,但是有没有更简洁的方法来做到这一点?
最佳答案
只需使用 return;
而不是 continue;
(不是 return false;
)。
关于javascript - 在jQuery的 `continue;`方法中实现 `each();`关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8455553/