each()方法遍歷jQuery對象,為每個選定的元素執(zhí)行一次函數(shù)。
each()方法旨在使DOM循環(huán)結(jié)構(gòu)簡潔明了且不易出錯。
$(selector).each(function(index, element))
遍歷每個列表項并彈出每個元素的文本:
$("button").click(function(){
$("li").each(function(){
alert($(this).text());
});
});測試看看?/?用于return false及終止each()循環(huán):
$("button").click(function(){
$("div").each(function(index, element){
$(element).css("backgroundColor", "yellow");
if($(this).is("#stop")){
$("span").text("div停止索引 #" + index);
return false;
}
});
});測試看看?/?| 參數(shù) | 描述 |
|---|---|
| function(index, element) | 指定要為每個選定元素執(zhí)行的函數(shù)
|