"drop_down_l-6ren">
gpt4 book ai didi

jquery - slick.js 错误 : "Uncaught TypeError: Cannot read property ' add' of null"

转载 作者:行者123 更新时间:2023-12-03 21:39:32 26 4
gpt4 key购买 nike

我使用 slick js 来实现图像的 slider View 。

这是我的代码。

<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>

现在使用这段代码我可以让 slider 正常工作,但它没有正常运行,我收到错误:

Uncaught TypeError: Cannot read property 'add' of null

最佳答案

这是由于 calling init twice 。这可以正常工作:

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

此外,“silder”看起来像是一个拼写错误。

依赖超时也容易出错。不幸的是,Internet Explorer uses a different event to tell you when the HTML elements and JS libraries have been loaded.有很多库可以避免 100 行左右的跨浏览器代码,但流行且相对较小的 jQuery 解决了计时问题,如下所示:

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

关于jquery - slick.js 错误 : "Uncaught TypeError: Cannot read property ' add' of null",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35884752/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com