doubi5520 2015-07-11 04:42
浏览 105

如何循环所有json数据并插入到我的HTML中?

I'd like to insert the Json data into my file.php. And so each group of them are insert into group of divs. (with the same style etc.)

I have a feeling the way I am using is not smart... better solutions are very welcome. Thanks guys.

    $.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);
    }
});
  • 写回答

2条回答 默认 最新

  • douhuan1979 2015-07-11 05:08
    关注

    You can write your code in flowing way. Uncountable date print in your loop. If I know your data format, I will give you exact way.

    Try this code.

    for (var prop in json.posts.data) {
          $("#description").html(json.posts.data[prop].message);
         // other same as
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害