<script>
$(function(){
// each方法: 可以用来遍历jq对象
// 语法:jq对象.each(function(index, ele){
// index 当前遍历的元素的下标
// ele 当前遍历的元素 ==> 是个dom对象
// })
/*const arr = ["lime", "yellow", "pink"];
const $lis = $("li");
$lis.each(function (index, ele) {
// console.log(this); // this其实就是ele
// console.log(index, ele);
// $(ele).css("backgroundColor", arr[index]);
$(this).css("backgroundColor", arr[index]);
})*/
// each除了可以遍历jq对象外,还可以遍历数组、对象
const arr = ["lime", "yellow", "pink"];
// 语法:$.each(数组或对象, function(index, ele){})
$.each(arr, function (index, ele) {
console.log(index, ele);
})
const obj = {
name: "lw",
age: 38,
skill: "翻墙"
}
$.each(obj, function (i, v) {
console.log(i, v);
})
})
</script>
jQuery - 方法 - each()
最新推荐文章于 2024-10-12 17:18:55 发布