【发布时间】:2016-04-14 06:38:25
【问题描述】:
我是 jQuery 的新手。我必须在我的网络应用程序中实现类似 Facebook 的功能,当用户滚动到底部时,会带来更多记录并附加/显示到页面。我已经能够在滚动到底部时成功发送 ajax 请求并获取记录。我从服务器带来的数据是完美的。但是我一直在弄清楚如何将该数据显示/附加到页面上。我尝试了一些东西但失败了。请提出建议。
我的 ajax 调用:
var noOfMessageLoadsDone = 1;
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() == $(document).height()) {
if(!$.active){
$.get(
"/fetchMoreOnScroll",
{loadsDone: noOfMessageLoadsDone},
function(data) {
for (var i = 0; i < data.length; i++) {
var datum = data[i];
var el = $("div .container").find("div .container").first().clone();
el.removeAttr("id");
el.attr("statusId", datum.id);
if(datum.statuserPicName){
el.children('img').attr("src", "/images/"+datum.statuserPicName);
}
el.find('p').text(datum.statuserFirstName+' '+datum.statuserLastName+': ');
$("div .container").find("div .container").append(el);
};
noOfMessageLoadsDone++;
}
);
}
}
});
还建议是否有比我尝试做的更优雅的解决方案来解决这个问题。任何帮助将不胜感激。
【问题讨论】: