サンプル
サンプル1
配列とオブジェクトを用意します。
配列は、繰り返しながら対応するidを持つdiv要素に、テキストとして自分のidを入れていきます。但し、ループ中に"four"が出てきた場合は処理を中断します。
オブジェクトは、KEYと同じidを持つ要素に、値を追記していきます。
<div id="one"></div>
<div id="two"></div>
<div id="three"></div>
<div id="four"></div>
<div id="five"></div>
var arr = [ "one", "two", "three", "four", "five" ];
var obj = { one:1, two:2, three:3, four:4, five:5 };
jQuery.each(arr, function() {
$("#" + this).text("My id is " + this + ".");
return (this != "four"); // will stop running to skip "five"
});
jQuery.each(obj, function(i, val) {
$("#" + i).append(document.createTextNode(" - " + val));
});
[
フレーム]