Error: Shuffle needs to be initialized with an element

Error: Shuffle needs to be initialized with an element

我有这段代码,但控制台给我一个错误给我一个错误 "Shuffle needs to be initialized with an element"。这是html

<div class="gallery-grid">
                <div class="gallery-item" data-date-created="2016-08-12">1</div>
                <div class="gallery-item" data-date-created="2016-08-13">2</div>
                <div class="gallery-item" data-date-created="2016-08-14">3</div>
                <div class="gallery-item" data-date-created="2016-08-15">4</div>
                <div class="gallery-item" data-date-created="2016-08-16">5</div>
                <div class="gallery-item" data-date-created="2016-08-17">6</div>
                <div class="gallery-item" data-date-created="2016-08-18">7</div>
                <div class="gallery-item" data-date-created="2016-08-19">8</div>
                <div class="gallery-item" data-date-created="2016-08-20">9</div>
                <div class="gallery-item" data-date-created="2016-08-21">10</div>
            </div>

JS

const shuffleInstance = new Shuffle(document.getElementsByClassName('gallery-grid'), {
          itemSelector: '.gallery-item',
          sizer: '.gallery-item',
          speed: 500,
          easing: 'ease-out'
      });

      shuffleInstance.Shuffle();

我做错了什么?

document.getElementsByClassName returns 具有给定 class 的所有元素的类似对象的数组,因此如果您想要第一个找到,则需要指定您想要的数组中的哪个项目gallery-grid 实例然后使用:

document.getElementsByClassName('gallery-grid')[0]

linasmnew 的解决方案对我不起作用。所以对于任何其他感兴趣的人:Javascript 有时在加载 HTML 文档之前被初始化(并且还没有要加载的元素)所以你必须包含

$(document).ready(function(){
//code here
} 

或不使用 jquery:

document.addEventListener("DOMContentLoaded", function(event) { 
//code here
});