使用FOR循环时未显示博客帖子

时间:2013-09-24 11:30:41

标签: javascript jquery html ajax

我有以下.js代码但是当我测试网站时,它只显示每个但没有内容的文字。我想在最后实现的是能够显示5个帖子,并在底部添加一个链接以显示更多的帖子:

$(document).ready(function(){
    url = 'http://hopexxx.com/category/daily-devotion/feed/';
    $.ajax({
        type: "GET",
        url: document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=1000&callback=?&q=' + encodeURIComponent(url),
        dataType: 'json',
        error: function(){
            alert('Unable to load feed, Incorrect path or invalid feed');
        },
        success: function(data){
            var postlist = data.responseData.feed.entries;
            var html = '<ul data-role="listview" data-filter="true">';
            for (var i = 0; i < 5; i++) {
                html += '<li>';
                html += '<a href="#">';
                html += '<div class="entry">entry.title</div>';
                html += '<div class="entry">author</div>';
                html += '<div class="entry">publishedDate</div>';
                html += '<div class="entry">contentSnippet</div>';
                html += '</a>';
                html += '</li>';
            }
            html += '</ul>';
            $("#postlist").append(html);
            $("#postlist ul[data-role=listview]").listview();

        }});
    });

2 个答案:

答案 0 :(得分:0)

在data.responseData.feed.entries上尝试$ .each循环,有关详细信息,请参阅here

答案 1 :(得分:0)

如果你知道你的帖子列表不是空的,并且每个条目都有属性'title','author','publishedDate'和'contentSnippet'试试这个:

var postlist = data.responseData.feed.entries;
var html = '<ul data-role="listview" data-filter="true">';
for (var i = 0; i < 5; i++) {

    var entry = postlist[i];

    html += '<li>';
    html += '<a href="#">';
    html += '<div class="entry">' + entry.title + '</div>';
    html += '<div class="entry">' + entry.author + '</div>';
    html += '<div class="entry">' + entry.publishedDate + '</div>';
    html += '<div class="entry">' + entry.contentSnippet + '</div>';
    html += '</a>';
    html += '</li>';
}
    html += '</ul>';
    $("#postlist").append(html);