繁体   English   中英

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

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

我有一个遍历动态表的函数,以查找突出显示的单元格。

在以下代码中,两个警报都将触发。 当“ previous”为1时,循环应在“ return false”处停止。

如何停止循环?

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");
       }
     });
}

使用break; 内部for循环,用户return false; 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