JQ中$.each的用法

jq中的each函数的用法数组

1.遍历对象函数

1.1 无参数this

$.each(obj, function(key, value){
    this    // this指当前属性的值
    key    // obj当前属性的名称
    value // 当前属性的值
});

1.2 有参数spa

$.each(obj, function(){
    this    // 这里的this 指向的是每次遍历中的obj当前的值
    p1  p2    // 访问附加参数
},['参数1', '参数2']);

2. 遍历数组code

2.1 无参数对象

$.each(arr, function(index, value){
    this      // this指向当前元素
    index    // i表示arr当前的下标
    value    // 表示的是当前的元素
});

2.2 有参数blog

$.each(arr, function(p1, p2){
    this    // 遍历到的当前的元素
    p1  p2    // 访问时候的附加的参数
}, ['参数1','参数2']);

 

示例three

1.遍历数组it

 var arr = [
   [1,4,2],
   [3,6,9],
   [5,6,1]  
];
$.each(arr,function(index, item){
    console.log(item[0]);    //  1, 3, 5
});

2.遍历对象io

var data = {
   one:1,
   two:2,
   three:3,
   four:4 
};

$.each(data, function( key, val ){
    console.log( data[key] );   // 1,2,3,4
});
相关文章
相关标签/搜索