jquery traversal method collection

1) each()
       
   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 value
            this is to get and traverse each dom object
        })
        $('div').each(function(i,item){
            i is the index value
            item 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]
}

    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324717391&siteId=291194637