javascript - jQuery 未捕获类型错误 : Infinite scroll

标签 javascript jquery

所以,我按照教程来实现无限滚动:http://www.infinite-scroll.com/infinite-scroll-jquery-plugin/comment-page-3/

在我的页脚中,我添加了 <script type="text/javascript" src="http://example.com/js/jquery.infinitescroll.min.js"></script>

在 javascript.js 文件中,我添加了以下内容(与教程相同):

 // infinitescroll() is called on the element that surrounds 
 // the items you will be loading more of
  $('#content').infinitescroll({

   navSelector  : "div.navigation",            
               // selector for the paged navigation (it will be hidden)
   nextSelector : "div.navigation a:first",    
               // selector for the NEXT link (to page 2)
   itemSelector : "#content div.post"          
               // selector for all items you'll retrieve
 });

但是,我得到 Uncaught TypeError: jQuery(...).infinitescroll is not a function错误。

因此,我添加了 js 文件并添加了与教程几乎相同的脚本。我可以看到页面上显示了 js 文件和脚本,但仍然出现错误。

有人能帮我解释为什么我会收到错误吗?

谢谢!

最佳答案

您应该确保您的初始化代码在 $(function(){}) 中,因为您需要在运行 infinitescroll 之前准备好 DOM。然后你必须确保你的页面中有一个 id="content"的元素。所以,确保你有这样的东西:

<div id="content">...</div>

<script src="jquery.infinitescroll.js"></script>

<script>
$(function() {
 // infinitescroll() is called on the element that surrounds 
 // the items you will be loading more of
  $('#content').infinitescroll({

   navSelector  : "div.navigation",            
               // selector for the paged navigation (it will be hidden)
   nextSelector : "div.navigation a:first",    
               // selector for the NEXT link (to page 2)
   itemSelector : "#content div.post"          
               // selector for all items you'll retrieve
 });
});
</script>

关于javascript - jQuery 未捕获类型错误 : Infinite scroll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32244445/

相关文章:

javascript - 在所有其他代码完成后,如何让这个函数运行?

javascript - CSS Transition 不适用于 FireFox 和 IE

javascript - 如何在命令行的每次调用中使用 Webpack 进行增量构建?

javascript - 如何附加到一个空的 JSON 对象?

javascript - 使用javascript隐藏一个div

javascript - 缩放时设置元素的重力?

javascript - angularjs:嵌套的ng-repeat在选择中创建空白值

jquery - Jssor Slider CSS 分离?

javascript - 我如何更改此 javascript 以使 div 停在它们的轨道上?

jquery - TinyMCE - 添加事件监听器问题