溫溫醬
2019-10-23 14:25:03
我有一些代碼:$(xml).find("strengths").each(function() { //Code //How can i escape from this block based on a condition.});如何根據(jù)條件從“每個”代碼塊中退出?更新:如果我們有這樣的事情怎么辦:$(xml).find("strengths").each(function() { $(this).each(function() { //I want to break out from both each loops at the same time. });});是否有可能從內(nèi)部的“每個”功能中同時脫離這兩個“每個”功能?#19.03.2013如果您想繼續(xù)而不是爆發(fā)return true;
3 回答

翻閱古今
TA貢獻(xiàn)1780條經(jīng)驗 獲得超5個贊
根據(jù)文檔,您可以簡單return false;地中斷:
$(xml).find("strengths").each(function() {
if (iWantToBreak)
return false;
});

拉丁的傳說
TA貢獻(xiàn)1789條經(jīng)驗 獲得超8個贊
從匿名函數(shù)返回false:
$(xml).find("strengths").each(function() {
// Code
// To escape from this block based on a condition:
if (something) return false;
});
從每種方法的文檔中:
從每個函數(shù)中返回“ false”將完全停止所有元素的循環(huán)(這就像在正常循環(huán)中使用“ break”一樣)。從循環(huán)內(nèi)返回“ true”會跳到下一個迭代(這就像對正常循環(huán)使用“ continue”一樣)。
添加回答
舉報
0/150
提交
取消