09-静态方法each方法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>09-静态方法each方法</title>
    <script src="JS_file/jquery-1.12.4.js"></script>
    <script>
        /*
        * 第一个参数:遍历到的元素
        * 第二个参数:当前遍历到的索引
        * 注意点:
        *原生的forEach只能遍历数组,不能遍历伪数组
        * */
        var arry=[1,3,5,7,9]  //数组
        var obj={0:1,1:3,2:5,3:7,4:9,length:5};//伪数组
        arry.forEach(function (value,index) {
            console.log(index,value);
        });

     //   obj.forEach(function (value,index) {        //报错,forEach is not a function
      //       console.log(index,value);
        //});
       //1.利用jQuery的each静态方法遍历数组
        /*
        *第一个参数:遍历到的索引
        *第二个参数:遍历到的元素
        * */
        $.each(arry,function (index,value) {
            console.log(index,value);
        })
        /*
        * 上面的obj伪数组,如果没有length这个属性,那么 console.log(typeof  index)返回的是string,若有,返回number
        * 即,没有length这个属性,那么obj就是一个json
        * */
        //2.利用jQuery的each静态方法遍历伪数组,是可以遍历的
        $.each(obj,function (index,value) {
            console.log(index,value);

        })
    </script>
</head>
<body>

</body>
</html>

猜你喜欢

转载自blog.csdn.net/yangyalun/article/details/82950536
今日推荐