數(shù)組遍歷:
var arr = [1, "hh", 5];
$(arr).each(function(index, item) {
// index: 下標(biāo); item: 元素值
// $(this): 獲取的是一個數(shù)組; item: 獲取的是一個對象
console.log(index, item, $(this));
});
對象遍歷:
var obj = {name: "hh", sex: "male", age: "18"};
方法一:
$.each(obj, function(i, con) {
// i: 屬性; con || obj[i]: 屬性值
// $(this): 獲取的值 == con.split("");
console.log(i, con, obj[i], $(this));
});
方法二:
for (j in obj) {
// i: 屬性; obj[j]: 屬性值
console.log(j, obj[j]);
}