jQuery uncaught TypeError:无限滚动

时间:2015-08-27 08:35:25

标签: 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文件和脚本,但仍然出现错误。

有人能帮助我,为什么我会收到错误吗?

谢谢!

1 个答案:

答案 0 :(得分:3)

您应该确保初始化代码在$(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>