$.getJSON与$.each结合使用:来获取json格式数据
在HTML页面中:
$.getJSON("ajax/rank_data.json",function(data){
var con=null;var ranking=null;
$.each(data,function(InfoIndex,Info){ //InfoIndex表示索引;Info["name"]
con="<div class=\"con\"><div class=\"portrait\" style=\"background:url("+Info["url"]+") no-repeat; background-position:center center;\"></div><p class=\"news\"><span class=\"username\"><strong>"+Info["name"]+"</strong></span><em>"+Info["class"]+"</em><span class=\"fen\"><strong>"+Info["score"]+"</strong>分</span></p></div>";
$(".rank_news").append(con);
if(InfoIndex>2){
ranking="<div class=\"ranking\"><strong>第"+(InfoIndex+1)+"名</strong></div>";
$(".sidebar").append(ranking);
}
});
});
在json页面中:
[{"name":"wen","class":"dsajf","score":"120"},{"name":"小小","class":"jyf","score":"100"}]