TypeError: $(...).lightGallery(...).destroy is not a function

TypeError: $(...).lightGallery(...).destroy is not a function

我从 firefox 收到这个错误:

TypeError: $(...).lightGallery(...).destroy is not a function

我正在使用以下插件来播放视频和显示图片库 https://github.com/sachinchoolur/lightGallery.

我想重新初始化 lightGallery,因为我正在使用 ajax 在容器中动态添加元素。它适用于以前版本的 lightGallery,但不适用于当前版本。

我正在使用以下代码。

// destroy previous gallery
$("#lightGallery2").lightGallery({
  selector : '.image_gallery',
  videojs: true,
  download: false
}).destroy();

// re-initialize
$("#lightGallery2").lightGallery({
  selector: '.image_gallery',
  videojs: true,
  download: false
});

求推荐。

谢谢

如果像大多数人那样写plugins/widgets,你应该这样调用destroy方法

$("#lightGallery2").lightGallery("destroy");

您必须使用以下方法销毁lightgallery version 1.2.x。

var $lg = $('#lightGallery2');

$lg.lightGallery({
  selector : '.image_gallery',
  videojs: true,
  download: false
});

$lg.data('lightGallery').destroy(true);

这里是docs

要销毁 data,您应该将数据属性添加到您的图库,然后销毁它,例如,如果您将图库应用到所有链接:

var myGallery = 'body',
    lightgallery = function() {
         $( myGallery ).attr('data-lightGallery', '1');
         $( myGallery ).lightGallery({selector: 'a[href$=".jpg"], a[href$=".jpeg"], a[href$=".png"], a[href$=".gif"]'});
    };

然后:

$( myGallery ).data('lightGallery').destroy(true);
lightgallery();

以下代码对我有用:

$lg.on('onBeforeClose.lg',function(event, index, fromTouch, fromThumb){
    try{$lg.data('lightGallery').destroy(true);}catch(ex){};
});

我只是创建了一个全局变量。它允许我执行所需的功能。

var PLUGIN; // global variable
...
if(PLUGIN) PLUGIN.data('lightGallery').destroy(true); // destroy 
PLUGIN = $("#lightGallery2").lightGallery();