jquery遍历方法合集

1)each()
       
   用于遍历任何对象
    $.each()方法接收两个参数,第一个是需要遍历的对象集合(json对象集合),第二个用来遍历的方法,这个方法又接受两个参数,第一个是遍历的index,第二个是当前遍历的参数的值。
        $.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>");
                    })
              })
        }
  • 选择器+遍历    
        $('div').each(function(i){
            i就是索引值
            this就是获取遍历每一个dom对象
        })
        $('div').each(function(i,item){
            i就是索引值
            item表示获取遍历的每一个dom对象
        })
        

3、更适用的遍历方法

1)先获取某个集合对象

2)遍历集合对象的每一个元素

        var d=$("div");

    $.each(d,function (index,domEle){

          d是要遍历的集合

          index就是索引值

          domEle 表示获取遍历每一个dom

    });

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]
}

    

猜你喜欢

转载自blog.csdn.net/qq_36306659/article/details/80054827
今日推荐