繰り返しで実行される関数 $.each(対象のオブジェクト, function(index, val) { 繰り返し実行する処理 });) array = ["aaa", "bbb", "ccc"]; $.each(array, function(index, val) { $('ul').append("li>" + index + ":" + val + "/li>"); });
$(function(){ $("#sample1btn").click(function(){ $( ".sample1box" ).each(function( i ) { $( this ).css("display","inline-block").fadeIn().fadeOut( 1000 * ( i + 1 ) ); }); }); });
sample2p
$(function(){ $("#sample2p").click(function () { $(this).each(function(){ if (this.style.color != "orange") { this.style.color = "orange"; } else { this.style.color = ""; } }); }); });
$("#s3Btn").on("click", function(){ $(".s3Div").each(function(i, element){ console.log(i + ": " + $(element).text()); }); $(".s3Div").each(function(i) { console.log(i + ": " + $(this).text()); }); });
$("#s4Btn").on("click", function(){ var s4Obj = {"ソヨゴ":"常緑広葉樹", "松":"常緑針葉樹", "イチョウ":"落葉針葉樹"}; $.each(s4Obj, function(key, val) { var s4Ul = document.createElement("ul"); s4Ul.style.listStyle = "none"; $("#s4Btn").before(s4Ul) $(s4Ul).append("li>" + key + '(' + val + ")/li>"); }); });
$("#s5Btn").on("click", function(){ var s5Array = ["紅葉", "山茶花", "アジサイ"]; $.each(s5Array, function(index, val){ var s5Ul = document.createElement("ul"); $("#s5Btn").before(s5Ul); $(s5Ul).append("li>" + index + "." + val + "/li>") .css("list-style-type", "none"); }); });