jQuery slick.js 中的错误:“未捕获的类型错误:无法读取 null 的属性‘添加’”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35884752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:04:39  来源:igfitidea点击:

Error in slick.js: "Uncaught TypeError: Cannot read property 'add' of null"

jqueryruby-on-railsslidernested-formsslick.js

提问by Pooja Mokariya

I used slick js for slider view of image.

我使用光滑的 js 来显示图像的滑块视图。

Here is my code.

这是我的代码。

<div class="slider_wrap add-remove">
    <%= f.fields_for :images do |image_form| %>
      <%#= render 'images_fields', :f => image_form %>
        <div>
          <%= image_tag image_form.object.picture.url,:class=>"drop_down_link even img_prev" %>
        </div>
        <div class="image_upload_div">
          <div class="image-upload">
            <label>
              <i class="fa fa-cloud-upload">
                <%= image_form.file_field :picture ,:'data-role'=>"none",:onchange=>"readURL(this);" , :accept => 'image/jpeg , image/png' %>
              </i>
            </label>
          </div>
        </div>
    <% end %>
    <%= f.link_to_add "Add a picture", :images ,:id=>"add_pic" ,:class=>"js-add-slide", :style=>"display: none;"%>
</div>

<script>
function silder(){
    slideIndex = 0;
      $('.add-remove').slick({
        slidesToShow: 2,
        slidesToScroll: 2
      });
      $('.js-add-slide').on('click', function() {
        $('.add-remove').slick('slickAdd');
      });

      $('.js-remove-slide').on('click', function() {
        $('.add-remove').slick('slickRemove');
      });
});
function readURL(input) {
  if (input.files && input.files[0]) {
    var reader = new FileReader();
    reader.onload = function (e) {
    $('.img_prev').last()
      .attr('src', e.target.result)
    };

    reader.readAsDataURL(input.files[0]);

    setTimeout(function(){
      $('#add_pic').trigger('click');
      silder();
    }, 100);
  }
}
</script>

Now with this code I got slider working, but its not coming proper I am getting error:

现在使用此代码,我可以使滑块正常工作,但它不正确,我收到错误消息:

Uncaught TypeError: Cannot read property 'add' of null

未捕获的类型错误:无法读取 null 的属性“添加”

回答by Cees Timmerman

That's due to calling init twice. This works without error:

这是由于两次调用 init。这工作没有错误:

$(".slider").not('.slick-initialized').slick()

Also, "silder" looks like a typo.

此外,“silder”看起来像是一个错字。

Relying on a timeout is also error-prone. Unfortunately, Internet Explorer uses a different event to tell you when the HTML elements and JS libraries have been loaded.There are many libraries to avoid the 100 or so lines of cross-browser code, but the popular and relatively small jQuery solves the timing issue like this:

依赖超时也容易出错。不幸的是,Internet Explorer 使用不同的事件来告诉您何时加载了 HTML 元素和 JS 库。有很多库可以避免 100 行左右的跨浏览器代码,但是流行且相对较小的 jQuery 解决了这样的计时问题:

$(function() {
  // Handler for .ready() called. Put the Slick Slider etc. init code here.
})