【问题标题】:Uncaught TypeError: $(...).lightGallery(...).destroy is not a function未捕获的 TypeError: $(...).lightGallery(...).destroy 不是函数
【发布时间】:2018-06-15 10:22:17
【问题描述】:

我正在使用我网站上的灯光图库插件,同时运行 $('#categoryImages').lightGallery().destroy(true);

我收到控制台错误

未捕获的 TypeError: $(...).lightGallery(...).destroy 不是函数

下面提到的代码 sn-p 是我用于灯光画廊的网站代码。

    $subcategory) { $subcategoryImage= scandir($dir.strtolower($getCategory)."/".$subcategory); unset($subcategoryImage[array_search('.', $subcategoryImage, true)]); unset($subcategoryImage[array_search('..', $subcategoryImage, true)]); foreach ($subcategoryImage as $key => $value2) { $data['subcategoryImages'][]= $dir.strtolower($getCategory)."/".$subcategory."/".$value2;?>
<li class="col-xs-6 col-sm-4 col-md-3" data-responsive=" 375, 480," data-src="">

<img class="img-responsive" src="">

jQuery 代码

$('body').on('click', '.category', function() {
    $('#categoryImages').lightGallery().destroy(true);
});

【问题讨论】:

标签: jquery lightgallery


【解决方案1】:

您需要保存对初始化插件的引用,然后在 ajax 完成后对其调用 destroy,在调用 destroy 后重新初始化插件

var gal = $('#categoryImages');
gal.lightGallery();
/...
request.done(function(data) {
//...
$('.ajxcategoryImages').html(subcategoryImages1); //append the results 
  gal.data('lightGallery').destroy(true);//destroy the current gallery
  gal.lightGallery();//create a new one
/..

【讨论】:

    【解决方案2】:

    作为文档应该是这样的:http://sachinchoolur.github.io/lightGallery/docs/api.html#methods

    var cI = $('#categoryImages');
    cI.lightGallery();
    $('body').on('click', '.category', function() {        
        cI.data('lightGallery').destroy(true);
    });
    

    更新小提琴:http://jsfiddle.net/n5w7eoxh/11/

    【讨论】:

    • 感谢您的回复,我使用了您分享的“未捕获的 TypeError:gal.destroy 不是函数”
    • 请在小提琴中提交您的完整代码,也许我可以帮助您
    • 嗨 Jagjeet Singh:感谢您的回复。我已经为我的网站使用了灯光图库,初始页面加载正常,点击多个复选框使用 ajax 获取与类别相关的图像。如果我选择了多个类别,则灯光库不起作用。但它没有在控制台中显示任何错误。
    • 不要在这里提交代码,请提供小提琴jsfiddle.net
    • 嗨 Jagjeet Singh:jsfiddle.net/gireesht14/n5w7eoxh 此链接将打开我的代码,但它与包含的动态 php 代码相关
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-15
    • 2020-04-15
    • 1970-01-01
    • 1970-01-01
    • 2023-01-14
    • 2020-10-05
    • 2019-09-15
    相关资源
    最近更新 更多