1) each()
is used to traverse any object
is used to traverse any object
The $.each() method accepts two parameters, the first is the collection of objects to be traversed (json object collection), the second is the method used to traverse, this method accepts two parameters, the first is the index of the traversal , the second is the value of the parameter currently traversed.
$.each(obj.results,function(index,item){
console.log(item);
});
function loadInfo(){
$.getJson("loadInfo" , function(data){
$('#info').html('');//先清空info内容
$.each(data , function(i ,item){$('#info').append("<div>" + item.id +"</div>" + "<div>" + item.username +"</div>");})
})
}
- selector + traversal
$('div').each(function(i){i is the index valuethis is to get and traverse each dom object})$('div').each(function(i,item){i is the index valueitem means to get each dom object traversed})3. More applicable traversal method
1 ) Get a collection object first
2 ) Iterate over each element of the collection object
var d=$("div");
$.each(d,function (index,domEle){
d is the collection to traverse
index is the index value
domEle means to get and traverse each dom pair
});
2)or(var i=0;i<stuListData.length;i++){ //循环遍历stuList
alert("age="+stuListData[i].age+" id="+stuListData[i].id+" name="+stuListData[i].name);
}3)for( key in c ){
//c[key]
}