
each()方法遍历jQuery对象,为每个选定的元素执行一次函数。
each()方法旨在使DOM循环结构简洁明了且不易出错。
语法:
$(selector).each(function(index, element))
实例
遍历每个列表项并弹出每个元素的文本:
$("button").click(function(){
$("li").each(function(){
alert($(this).text());
});
});测试看看‹/›用于return false及终止each()循环:
$("button").click(function(){
$("div").each(function(index, element){
$(element).css("backgroundColor", "yellow");
if($(this).is("#stop")){
$("span").text("div停止索引 #" + index);
return false;
}
});
});测试看看‹/›参数值
| 参数 | 描述 |
|---|---|
| function(index, element) | 指定要为每个选定元素执行的函数
|