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
});
相關文章
相關標籤/搜索