简体   繁体   English

返回false不会停止值为1的循环

[英]Return false won't stop loop with value of 1

I have a function that loops through a dynamic table, looking for highlighted cells. 我有一个遍历动态表的函数,以查找突出显示的单元格。

In the following code both alerts trigger. 在以下代码中,两个警报都将触发。 The loop should stop at "return false", when "previous" is 1. 当“ previous”为1时,循环应在“ return false”处停止。

How can I stop the loop? 如何停止循环?

for (i = 2; i < 5; i++) {
    $("#tablegrid").find("td:nth-child("+i+")").each(function() {
       if ($(this).hasClass("highlighted")) {
         var previous = i-1;

         if (previous===1) {
           alert("loop should now stop");
           return false;
         }

         alert("loop has continued");
       }
     });
}

Use break; 使用break; inside for loop and user return false; 内部for循环,用户return false; inside each loop to stop in the middle. each循环内部停止在中间。

 for (i = 0; i < 10; i++) { if (i===5) { alert("loop should now stop"); nextFunction(); break; } alert("loop has continued"); } function nextFunction(){ alert("Next Function"); $('.box').each(function(){ if($(this).index() == 4){ alert("loop should now stop"); anotherFunction(); return false; } alert("each has continued"); }) } function anotherFunction(){ alert("another function"); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM