jQuery的each可以遍历伪数组

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>09-静态方法的each方法</title>
<script src="../js/jquery-3.4.1.js"></script>
<script>
var arr=[1,3,5,7,9];
var obj={0:1,1:3,2:5,3:7,4:9,length:5};
/*

第一个参数:遍历到的元素
第二个参数:当前遍历到的索引
注意点:原生的forEach方法只能便利数组:不能遍历伪数组
*/
/* arr.forEach(function (value, index) {
console.log(index,value);
})*/
/*obj.forEach(function (value,index) {
console.log(index);//报错
})*/

//利用jQuery静态的each方法
/**
* 第一个参数:遍历到的索引
* 第二个参数:遍历到的元素
* 注意点:
* jQuery的each方法可以遍历伪数组的
*/
/* $.each(arr,function (index,value) {
console.log(index,value);
});*/
$.each(obj,function (index,value) {
console.log(index,value);
});

</script>
</head>
<body>

</body>
</html>

猜你喜欢

转载自www.cnblogs.com/god1/p/12391369.html