javascript - 同位素 - 无法读取未定义的属性 'filteredItems'

标签 javascript jquery function console typeerror

我正在尝试让我的同位素帖子页面与加载更多 按钮一起使用(如此处所示:https://codepen.io/bebjakub/pen/jWoYEO)。我有在 Codepen 上运行的代码,但我无法让它在网站上运行。

工作代码笔(我的(过滤和加载更多)- https://codepen.io/whitinggg/pen/qyvVwz

实时页面链接 - Here

我目前在控制台中看到关于我的 isotope.js 文件的错误:

Uncaught TypeError: Cannot read property 'filteredItems' of undefined
    at loadMore (isotope.js?v=2.2.7:53)
    at HTMLDocument.<anonymous> (isotope.js?v=2.2.7:48)
    at i (jquery.js?ver=1.12.4:2)
    at Object.fireWith [as resolveWith] (jquery.js?ver=1.12.4:2)
    at Function.ready (jquery.js?ver=1.12.4:2)
    at HTMLDocument.K (jquery.js?ver=1.12.4:2)

任何人都可以帮助解决此错误的含义吗?请帮助我解决它。

我的 Isotope.js 文件

jQuery(function ($) {


    // with jQuery
    var $container = $('.grid').isotope({
      itemSelector: '.grid-item',
      layoutMode: 'packery',
      columnWidth: '.grid-sizer',
      packery: {
        gutter: '.gutter-sizer'
      }
    });

     //Add the class selected to the item that is clicked, and remove from the others
     var $optionSets = $('#filters'),
     $optionLinks = $optionSets.find('a');

     $optionLinks.click(function(){
     var $this = $(this);
     // don't proceed if already selected
     if ( $this.hasClass('selected') ) {
       return false;
     }
     var $optionSet = $this.parents('#filters');
     $optionSets.find('.selected').removeClass('selected');
     $this.addClass('selected');

     //When an item is clicked, sort the items.
     var selector = $(this).attr('data-filter');
     $container.isotope({ filter: selector });

     return false;
     });

    // layout Isotope after each image loads
    $container.imagesLoaded().progress( function() {
      $container.isotope('layout');
    });


//****************************
  // Isotope Load more button
  //****************************
  var initShow = 15; //number of items loaded on init & onclick load more button
  var counter = initShow; //counter for load more button
  var iso = $container.data('grid'); // get Isotope instance

  loadMore(initShow); //execute function onload

  function loadMore(toShow) {
    $container.find(".hidden").removeClass("hidden");

    var hiddenElems = iso.filteredItems.slice(toShow, iso.filteredItems.length).map(function(item) {
      return item.element;
    });
    $(hiddenElems).addClass('hidden');
    $container.isotope('layout');

    //when no more to load, hide show more button
    if (hiddenElems.length == 0) {
      jQuery("#load-more").hide();
    } else {
      jQuery("#load-more").show();
    };

  }

  //append load more button
  $container.after('<button id="load-more"> Load More</button>');

  //when load more button clicked
  $("#load-more").click(function() {
    if ($('#filters').data('clicked')) {
      //when filter button clicked, set initial value for counter
      counter = initShow;
      $('#filters').data('clicked', false);
    } else {
      counter = counter;
    };

    counter = counter + initShow;

    loadMore(counter);
  });

  //when filter button clicked
  $("#filters").click(function() {
    $(this).data('clicked', true);

    loadMore(initShow);
  })


});

最佳答案

为什么它在 codepen 上工作而在我的网站上没有工作的背后的问题原来是一个简单的问题。不记得为什么我现在这样做了,但我已经从 Codepen 到站点稍微更改了一行代码,这似乎导致了这个问题。那一行是:

var iso = $container.data('grid'); // get Isotope instance

应该是:

var iso = $container.data('isotope'); // get Isotope instance

关于javascript - 同位素 - 无法读取未定义的属性 'filteredItems',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51981689/

相关文章:

javascript - 强制下载文件而不是在浏览器中显示

Javascript:对象中的函数根据声明语法自动命名(或不命名)——为什么?

javascript - 在 node.js 中获取模块的名称

javascript - 如何同时实现 Javascript Promise.all 有趣的功能

javascript - 如何从 React Native javascript 语法切换到 es6

javascript - js 文件的简单合并导致错误和 jquery 未定义

javascript - 如何让 openlayers map 显示在 twitter bootstrap 中?

javascript - 如何在图像 map 中组合热点?

Javascript - 传递预定义值

c - 如何在c中的函数中手动输入数组?