[英]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.