为了账号安全,请及时绑定邮箱和手机立即绑定

如何从JQuery中的each()函数中断/退出?

如何从JQuery中的each()函数中断/退出?

温温酱 2019-10-23 14:25:03
我有一些代码:$(xml).find("strengths").each(function() {   //Code   //How can i escape from this block based on a condition.});如何根据条件从“每个”代码块中退出?更新:如果我们有这样的事情怎么办:$(xml).find("strengths").each(function() {   $(this).each(function() {       //I want to break out from both each loops at the same time.   });});是否有可能从内部的“每个”功能中同时脱离这两个“每个”功能?#19.03.2013如果您想继续而不是爆发return true;
查看完整描述

3 回答

?
翻阅古今

TA贡献1780条经验 获得超5个赞

根据文档,您可以简单return false;地中断:


$(xml).find("strengths").each(function() {


    if (iWantToBreak)

        return false;

});


查看完整回答
反对 回复 2019-10-23
?
拉丁的传说

TA贡献1789条经验 获得超8个赞

从匿名函数返回false:


$(xml).find("strengths").each(function() {

  // Code

  // To escape from this block based on a condition:

  if (something) return false;

});

从每种方法的文档中:


从每个函数中返回“ false”将完全停止所有元素的循环(这就像在正常循环中使用“ break”一样)。从循环内返回“ true”会跳到下一个迭代(这就像对正常循环使用“ continue”一样)。


查看完整回答
反对 回复 2019-10-23
  • 3 回答
  • 0 关注
  • 842 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信