【发布时间】:2013-03-30 07:35:16
【问题描述】:
我可以实现 jQuery.each,其中如果我在 jQuery.each 中处理的数组中找到任何匹配项,那么我可以中断/返回而无需进一步处理剩余元素。
<ul>
<li>foo</li>
<li>bar</li>
您可以选择列表项并遍历它们:
$( "li" ).each(function( index ) {
//if($(this).text() == 'foo') , i.e if foo is found ,
//then return instead of processing bar.
});
【问题讨论】:
-
当你有这样的问题时,你应该考虑阅读the documentation:“我们可以通过让回调函数返回 false 来打破 $.each() 循环在特定的迭代。返回非-false 与 for 循环中的 continue 语句相同;它将立即跳到下一次迭代。"
标签: jquery