Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

javascript - 如何从JQuery中的each()函数中断/退出? [重复](How to break/exit from a each() function in JQuery? [duplicate])

This question already has an answer here:

(这个问题已经在这里有了答案:)

I have some code:

(我有一些代码:)

$(xml).find("strengths").each(function() {
   //Code
   //How can i escape from this block based on a condition.
});

How can i escape from the "each" code block based on a condition?

(如何根据条件从“每个”代码块中退出?)

Update:(更新:)

What if we have something like this:

(如果我们有这样的事情怎么办:)

$(xml).find("strengths").each(function() {
   $(this).each(function() {
       //I want to break out from both each loops at the same time.
   });
});

Is it possible to break out from both "each" functions from the inner "each" function?

(是否有可能从内部的“每个”功能中同时脱离这两个“每个”功能?)

# 19.03.2013

(#19.03.2013)

If you want to continue instead of break out

(如果您想继续而不是爆发)

return true;
  ask by Tebo translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

According to the documentation you can simply return false;

(根据文档,您可以简单地return false;)

to break:

(打破:)

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

    if (iWantToBreak)
        return false;
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...