1、沒有參數
$("img").each(function(){ $(this).toggleClass("example"); });
- 1
- 2
- 3
2、有一個參數,這個參數為index
$("img").each(function(i){ this.src = "test" + i + ".jpg"; });
- 1
- 2
- 3
3、有兩個參數,第一個參數為index,第二個參數為dom元素本身
$("button").click(function () { $("div").each(function (index, domEle) { // domEle == this $(domEle).css("backgroundColor", "yellow"); if ($(this).is("#stop")) { $("span").text("Stopped at div index #" + index); return false; } }); });
