首页 > 解决方案 > Javascript:无限滚动后更新文档

问题描述

在可无限滚动的页面上,文档元素不会反映附加到页面底部的新内容。

console.log(document.body.innnerHTML); // prints the document on initial page load
var scrollingElement = (document.scrollingElement || document.body);
    scrollingElement.scrollTop = scrollingElement.scrollHeight;
console.log(document.body.innnerHTML); // does not print document with new content

PS:

可测试https://news.google.com/

标签: javascriptdom

解决方案


我在这里有几点说明为什么您的调查可能是错误的,而不是它的结果:

  1. 您在 innerHTML 中有一个错字(一个n太多了)
  2. 您可能只是没有看到代码中发生变化的那部分,但它就在那里。如果你这样做:

    const x = document.body.innerHTML;
    document.scrollingElement.scrollTop = document.scrollingElement.scrollHeight;
    console.log(x == document.body.innerHTML);
    

    你可以看到这是false

  3. https://news.google.com/似乎不能无限滚动。至少不是我如何使用它。总之,2.显示有区别

推荐阅读