jquery $.each()でbreak、continue
$.each()内で「break、continue」をするときは「return」で「true / false」を使う
作成日:2018-02-26, 更新日:2018-09-13
基本
・break - 「return false;」を使う
・continue - 「return true;」を使う
サンプル
サンプルソース
function sampleEach(isContinue) { var cnt = ''; var ary = ['a', 'b', 'c']; $.each(ary, function(){ cnt += this + ':開始、'; if ( this == 'b' ) { cnt += '<br />'; return isContinue; } cnt += this + ':終了<br />'; }); return cnt; } $('#cntT').html(sampleEach(true)); // 「continue(return true;)」 $('#cntF').html(sampleEach(false)); // 「break(return false;)」
サンプルの結果
内容 | 記述 | 結果 |
---|---|---|
continue | return true; | a:開始、a:終了 b:開始、 c:開始、c:終了 |
break | return false; | a:開始、a:終了 b:開始、 |