簡體   English   中英

類型錯誤:$(...).lightGallery(...).destroy 不是函數

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM