简体   繁体   English

如何在不使用 Jquery 滚动到页面底部的情况下从源 html 页面加载子 html 页面内容?

[英]How to load child html page content from source html page without scrolling to bottom of the page using Jquery?

I am using Jquery to load a child html page.我正在使用 Jquery 加载子 html 页面。 The child html page has a list of div's and the numbers can vary up to 50 div's.子 html 页面有一个 div 列表,数字最多可以变化 50 个 div。 I am using Jquery.get to load the child html in parent.我正在使用 Jquery.get 在父级中加载子级 html。 i have a requirement to view the content of the child containing all div's without scrolling to bottom of the page.我需要在不滚动到页面底部的情况下查看包含所有 div 的子项的内容。 I tried to use the below code but the div's from the child keep repeating after all the div's are displayed.我尝试使用下面的代码,但是在显示所有 div 后,孩子的 div 会不断重复。 Can anyone please let me know how to achieve this?谁能让我知道如何实现这一目标?

Parent.html父.html

<div id="parent"></div>

<script>
    $.get('child.html', function(data){ // Loads content into the 'data' variable.
        $('#parent').append(data); // Injects 'data' after the #div element.                        
    });

    $(document).scroll(function(e){             
        if ($(window).scrollTop() >= ($(document).height() - $(window).height())*0.7){

            $.get('child.html', function(data){ // Loads content into the 'data' variable.
                $('#parent').append(data); // Injects 'data' after the #div element.                        
            });                                 
        }

    });
</script>

Child.html子.html

<div class='row list' id=1>
    <span>1</span>
</div>

<div class='row list' id=2>
    <span>2</span>
</div>

<div class='row list' id=3>
    <span>3</span>
</div>

<div class='row list' id=4>
    <span>4</span>
</div>

<div class='row list' id=5>
    <span>5</span>
</div>

在此处输入图像描述

If I understand correctly, you want to load 50 DIV Elements from the child.html file at a time.如果我理解正确,您想一次从child.html文件加载 50 个 DIV 元素。 To do this, we need to track how many items have been loaded and then filter the HTML before loading / appending it to the document.为此,我们需要跟踪已加载的项目数量,然后在加载/附加到文档之前过滤 HTML。

We want to create a Flow / State diagram to understand what is needed.我们想创建一个 Flow / State 图来了解需要什么。

  1. Initial Load初始负载
    • Populate parent element with first 50 items from child.html使用 child.html 的前 50 个项目填充parent元素
  2. User reads content, scrolls down page用户阅读内容,向下滚动页面
  3. User reaches 70% of document height用户达到文档高度的 70%
    • Request next 50 elements请求接下来的 50 个元素
    • Append next result set to parent Append 下一个结果集parent

Note: It would be preferable to use a Server Side Script , like PHP, that can serve up the data in chunks.注意:最好使用服务器端脚本,例如 PHP,它可以以块的形式提供数据。

As I understand it currently, child.html is static and contains N number of elements.据我目前了解, child.html是 static 并包含 N 个元素。 JS/jQuery has no way to load a specific number of items. JS/jQuery 无法加载特定数量的项目。 The entire document will be loaded each time.每次都会加载整个文档。 Using .load() , we can filter for a specific selector.使用.load() ,我们可以过滤特定的选择器。

The .load() method, unlike $.get() , allows us to specify a portion of the remote document to be inserted. .load()方法与$.get()不同,它允许我们指定要插入的远程文档的一部分。 This is achieved with a special syntax for the url parameter.这是通过 url 参数的特殊语法实现的。 If one or more space characters are included in the string, the portion of the string following the first space is assumed to be a jQuery selector that determines the content to be loaded.如果字符串中包含一个或多个空格字符,则假定字符串中第一个空格之后的部分是确定要加载的内容的 jQuery 选择器。

I thought we could use :lt() , like so:我以为我们可以使用:lt() ,如下所示:

$("#parent").load("child.html.row:lt(50)"); $("#parent").load("child.html.row:lt(50)");

This is deprecated:这已被弃用:

As of jQuery 3.4, the :lt pseudo-class is deprecated.从 jQuery 3.4 开始,不推荐使用:lt伪类。 Remove it from your selectors and filter the results later using .slice() .将其从选择器中删除,稍后使用.slice()过滤结果。 For example, :lt(3) can be replaced with a call to .slice( 0, 3 ) .例如, :lt(3)可以替换为对.slice( 0, 3 )的调用。

Do this, we would then revert back to $.get() like so.这样做,我们会像这样恢复到$.get()

$.get("child.html", function(ht){
  $("#parent").append($(ht).find(".row").slice(0,50));
});

This will work yet is not optimal as the GET request will return the entire document each time.这将起作用但不是最佳的,因为 GET 请求每次都会返回整个文档。 If this is the case, it would be best to load the entire document initially and then reveal more items as the User scrolls down.如果是这种情况,最好先加载整个文档,然后在用户向下滚动时显示更多项目。 This defeats the purpose.这违背了目的。

If you want to try it, here is an example:如果你想尝试一下,这里有一个例子:

$(function() {
  var count = 0;

  function getNextSet(url, n) {
    var ht;
    $.get(url, function(data) {
      var start = count;
      count = count + n;
      ht = $(data).find(".row").slice(start, count);
    });
    return ht;
  }

  $("#parent").append(getNextSet("child.html", 50));

  $(document).scroll(function() {
    if ($(window).scrollTop() >= ($(this).height() - $(window).height()) * 0.7) {
      $("#parent").append(getNextSet("child.html", 50));
    }
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM