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;
}
});
});
本文介绍使用jQuery库操作DOM元素的方法,包括切换样式类、修改图片源及改变背景颜色等常见操作。通过三个具体实例展示了如何利用$.each()方法遍历DOM元素并执行不同任务。
1万+

被折叠的 条评论
为什么被折叠?



