2016-05-09 13 views
0

我正在使用jpoint的航點插件。它適用於頁面上的div。但是當我使用AJAX添加新的div時,它不適用於那些新添加的div。它不算他們。如何讓jQuery waypoint插件計算新增內容?

這裏是我的代碼:

Waypoint = $('.Picture-1A').waypoint(function(direction){ 
    if(direction == 'down'){ 
     id = $(this.element).children(".PictureID").text(); 
     $.post('ajax/count-pictures-views.php', {picture_id: id}, function(data){ 
      Waypoint.refreshAll(); 
     }); 
    } 
}); 

我也用在(),但它並沒有爲所有的工作,默認和新增。 我如何才能使它適用於新添加的內容?

這是我的Ajax頁面:

$(document).ready(function(){ 

endPictures = true; 

scrollCount = 0; 

$(window).scroll(function(){ 

    if(endPictures == true){ 

    if(scrollCount == 0){ 

     if($(window).scrollTop() >= $(document).height() - $(window).height() - 3000){ 

     scrollCount = 1; 

     picturesCount = $(".Picture-1A").length; 

      $.post('ajax/load-latest.php', {off_set:picturesCount}, function(data){ 
       if(data){ 
        $("#loadMore").append(data); 
        scrollCount = 0; 
       }else{ 
        $("#loadMore").append("<div class=\"NoMorePictures\"><center>There are no pictures left.</center></div><br/ >") 
        endPictures = false; 
        scrollCount = 0; 
       } 
      }); 

     } 

    } 

    } 
});}); 
+0

你能顯示完整的詳細信息代碼如何添加內容? – user5200704

+0

@ user5200704我已經添加了Ajax腳本。我認爲問題出在'$ .waypoints('refresh')'上。我認爲使用它可以解決問題,但我不知道應該把它放在腳本中。 – FarrisFahad

+0

使用$ .waypoints('refresh')後添加新內容時,您可以添加ajax函數。 – user5200704

回答

0

我終於想出了一個解決方案。這是我認爲它是自我解釋的代碼。

$.post('ajax/load-latest.php', {off_set:picturesCount}, function(data){ 
    if(data){ 
     $("#loadMore").append(data); 
     scrollCount = 0; 
     $('.Picture-1A').waypoint(function(direction){ 
      if(direction == 'down'){ 
       id = $(this.element).children(".PictureID").text(); 
       $.post('ajax/count-pictures-views.php', {picture_id: id}, function(data){ 

       }); 
      } 
     }); 
    }else{ 
     $("#loadMore").append("<div class=\"NoMorePictures\"><center>There are no pictures left.</center></div><br/ >") 
        endPictures = false; 
        scrollCount = 0; 
       } 
      }); 

我所做的只是將$('.Picture-1A').waypoint(function(){});添加到無限滾動ajax調用。所以每次觸發ajax調用時,都會刷新路點。

+0

這是正確的,除非您最終將多個航點添加到前一個'.Picture-1A'元素。你只想找到新添加的那些。也許通過在他們上面設置一個班級,然後用':not'添加一個航點並跳過它們。 – imakewebthings

+0

@imakewebthings號不錯。它不會計算兩次。我測試過了。但是,謝謝你的回覆:)並感謝你的插件。 – FarrisFahad