【问题标题】:how to display/append more records to scroll to bottom using jQuery如何使用jQuery显示/附加更多记录以滚动到底部
【发布时间】: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++;
    }
   );
  }  
 }
 });

还建议是否有比我尝试做的更优雅的解决方案来解决这个问题。任何帮助将不胜感激。

【问题讨论】:

    标签: jquery html css


    【解决方案1】:

    我相信你已经阅读了关于 jQuery 的 append 文档。在您的情况下,附加应该发生在“.container”的末尾。但是,在您的选择器中,您似乎在“div”和“.container”之间有一个空格,这表明“.container”需要成为“div”元素的子元素才能找到它。尝试使用选择器“div.container”。

    【讨论】:

    • 我现在正在改变方法。我正在创建一个示例div,其中包含所有标签但为空。我将选择这个div,克隆它,填充它,然后将它附加到$(div.container) 的末尾。感谢您增加我对空间意味着孩子的认识。 :)
    猜你喜欢
    • 1970-01-01
    • 2012-06-24
    • 2016-04-07
    • 2023-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-25
    • 1970-01-01
    相关资源
    最近更新 更多