![](/img/trans.png)
[英]using lightGallery and Jekyll: TypeError: $(...).lightGallery 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
});
请建议。
谢谢
以下代码对我有用:
$lg.on('onBeforeClose.lg',function(event, index, fromTouch, fromThumb){
try{$lg.data('lightGallery').destroy(true);}catch(ex){};
});
如果它是像大多数插件/小部件一样编写的,则应该这样调用destroy方法。
$("#lightGallery2").lightGallery("destroy");
您必须使用以下方法来销毁lightgallery 1.2.x版。
var $lg = $('#lightGallery2');
$lg.lightGallery({
selector : '.image_gallery',
videojs: true,
download: false
});
$lg.data('lightGallery').destroy(true);
这是文档
对于销毁数据,您应该将数据属性添加到图库中,然后销毁它,例如,如果您将图库应用于所有链接:
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();
我只是创建了一个全局变量。 它允许我执行所需的功能。
var PLUGIN; // global variable
...
if(PLUGIN) PLUGIN.data('lightGallery').destroy(true); // destroy
PLUGIN = $("#lightGallery2").lightGallery();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.