2013-10-23 247 views
0

我添加一個LoadMore函數來根據當前顯示帖子的長度和DOM中的帖子總數追加更多的帖子。我遇到的問題是當我登錄listofposts控制檯並檢查Google Chrome中的元素時,我看到長度顯示爲零(0)。我不確定我到底出錯的地方,或者我採取的措辭是對的,還是應該先分開這兩個函數,首先加載前4個帖子,然後創建一個獨立的新函數來處理追加?加載更多的帖子不工作

$(document).on('pagebeforeshow', '#blogposts', function() {  
    //$.mobile.showPageLoadingMsg();  
     $.ajax({ 
      url: "http://howtodeployit.com/category/daily-devotion/?json=recentstories&callback=", 
      dataType: "json", 
      jsonpCallback: 'successCallback', 
      async: true, 
      beforeSend: function() { $.mobile.showPageLoadingMsg(true); }, 
      complete: function() { $.mobile.hidePageLoadingMsg(); }, 
      success:function(data){ 

      var $listofposts = $('data'); 
      console.log($listofposts); 
      var $loadMore = $listofposts.parent().find('.load-more'); 
      // console.log($loadMore); 

      currentPage = 0; 
      postsPerPage = 4; 

      var showMorePosts = function() { 

       $offset = currentPage * postsPerPage, //initial value is 0 

        posts = data.posts.slice($offset, $offset + postsPerPage); 
        console.log(posts); 
        $.each(posts, function(i, val) { 
        //console.log(val); 
        $("#postlist").html(); 
        var result = $('<li/>').append([$("<h3>", {html: val.title}),$("<p>", {html: val.excerpt})]).wrapInner('<a href="#devotionpost" onclick="showPost(' + val.id + ')"></a>'); 
        $('#postlist').append(result); 
        console.log(result); 
        }); 

        if(posts.length !== postsPerPage){ 
         alert ('True'); 
         $loadMore.hide(); 
        } 

        currentPage++; 
        $("#postlist").listview(); 
        $("#postlist").listview('refresh'); 
        } 

        showMorePosts(); 
        $loadMore.on('click', showMorePosts); 

       }}); 

回答

1
var $listofposts = $('data'); 

是要求的jQuery文檔中的所有<data>標籤的列表。 您可能想改爲使用$(data)

+0

啊我想我一定在睡覺。這是正確的。現在還有一件事,我怎麼顯示元素'count',因爲它在object元素中。該長度當前顯示1,但計數正在顯示帖子的確切數量11 – Chelseawillrecover

+0

我想你會希望找到HTML元素,你想顯示帖子數,並在其jQuery上調用像'text(data.count)'的東西目的。 –

+0

我該如何做到這一點。仍然是Jquery的新功能 – Chelseawillrecover