我想插入Json數據到我的file.php。所以他們的每一組都插入到一組div中。 (具有相同的風格等)如何循環所有json數據並插入到我的HTML中?
我有一種感覺,我使用的方式不聰明......更好的解決方案是非常受歡迎的。多謝你們。
$.ajax({
url: feedURL,
jsonpCallback: 'jsonpCallback',
contentType:"application/json",
dataType: 'jsonp',
success: function(json) {
for (var i=0; i < 15; i++) {
var date = new Date(json.posts.data[i].created_time);
var months = ["/01/", "/02/", "/03/", "/04/", "/05/", "/06/", "/07/",
"/08/", "/09/", "/10/", "/11/", "/12/"];
$("#description").html(json.posts.data[i].message);
$("#caption").html(json.posts.data[i].caption);
$("#expire_date").html(date.getDate() + months[date.getMonth()] + date.getFullYear());
$("#fb_link").html(
'<a href="' + json.posts.data[i].link + '">'
+ 'Total Like: ' + json.posts.data[i].likes.summary.total_count
+ ' Total Share: ' + json.posts.data[i].shares.count
+ ' View on Facebook' + '</a>'
);
$("#profile_img").html('<img src="' + json.picture.data.url + '">');
$("#profile_name").html(json.name);
$("#full_fb_photo").html('<img src="' + json.posts.data[i].full_picture + '">');
}
},
error: function(e) {
console.log(e.message);
}
});
成功函數裏''json''的輸出是什麼?並且使用像#description一樣的DOM元素將覆蓋prev值。所以即使你獲得了很多記錄,也只會顯示最後一條記錄。 –
hi @SameerK感謝您的快速響應。你完全正確,結果只顯示最後一個。 Json的輸出是很多數據,鍵數從0到14,每個數據都有時間,網址等。 – Yan
向你展示json響應。爲了顯示所有的記錄,你需要在循環內部建立一部分html,然後使用容器的ID追加到主html中的某個容器。 –