Programing

JQuery에서 each () 함수를 중단 / 종료하는 방법은 무엇입니까?

lottogame 2020. 10. 3. 09:43
반응형

JQuery에서 each () 함수를 중단 / 종료하는 방법은 무엇입니까? [복제]


이 질문에 이미 답변이 있습니다.

몇 가지 코드가 있습니다.

$(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;

문서 에 따르면 간단하게 다음 return false;을 중단 할 수 있습니다 .

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

    if (iWantToBreak)
        return false;
});

당신이 사용할 수있는 return false;

+----------------------------------------+
| JavaScript              | PHP          |
+-------------------------+--------------+
|                         |              |
| return false;           | break;       |
|                         |              |
| return true; or return; | continue;    |
+-------------------------+--------------+

익명 함수에서 false를 반환합니다.

$(xml).find("strengths").each(function() {
  // Code
  // To escape from this block based on a condition:
  if (something) return false;
});

각 방법 의 문서에서 :

Returning 'false' from within the each function completely stops the loop through all of the elements (this is like using a 'break' with a normal loop). Returning 'true' from within the loop skips to the next iteration (this is like using a 'continue' with a normal loop).


if (condition){ // where condition evaluates to true 
    return false
}

see similar question asked 3 days ago.

참고URL : https://stackoverflow.com/questions/1799284/how-to-break-exit-from-a-each-function-in-jquery

반응형