ajax循环输出数据通常用的2种方式

 1 <script>  
 2             $(function(){  
 3                 $("#btn").click(function(){  
 4                     $.ajax({  
 5                         type:"get",  
 6                         dataType:"json",  
 7                         url:"data.json",  
 8                         data:{},  
 9                         success:function(res){  
10                             $(".alert").html("请求成功")                   
13                             var html='';  
16                             //第一次方式  
17                 //   $.each(res,function(i,v){  
18                 //     html+="<li data-id="+v.id+">"+v.name+"</li>"  
19                 //   })  

20 //第二次方式 21 for(var i=0;i<res.length;i++){ 22 html+="<li data-id="+res[i].id+">"+res[i].name+"</li>" 23 } 24 25 26 $(".list").html(html) 27 28 }, 29 error:function(){ 30 $(".alert").html("请求失败") 31 } 32 }) 33 }) 34 $("#clear").click(function(){ 35 $(".list").html("") 36 $(".alert").html("") 37 }) 38 39 }) 40 </script>
原文地址:https://www.cnblogs.com/huanyumingmei/p/8495539.html