javascript 猫头鹰传送带销毁问题(未捕获的类型错误:无法读取未定义的属性“销毁”)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23217594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 00:33:29  来源:igfitidea点击:

Owl carousel destroy issue (Uncaught TypeError: Cannot read property 'destroy' of undefined )

javascriptjqueryhtmlcss

提问by Prakash Upadhyay

I am using Owl carousel for my project on certain resolution i need to destroy the owl carousel so i have used owl carousel destroy function but while using the function other jquery function is not working not even document ready Please help to fix this issue so that i can use the owl carousel destroy function along with jquery

我在我的项目中使用 Owl carousel 以某些分辨率我需要销毁 owl carousel 所以我使用了 owl carousel destroy 功能但是在使用该功能时其他 jquery 功能甚至无法工作甚至没有准备好文档请帮助解决这个问题,以便我可以使用 owl carousel 销毁功能和 jquery

here is my code

这是我的代码

  function mobile() {

    var checkWidth = $(window).width();
    var banner = $("#ndmv-banner-intro");

      if(checkWidth >980){

        banner.owlCarousel({
        singleItem:true,
        autoPlay:false,
        dragBeforeAnimFinish : true
       });

        }else{
           banner.data('owlCarousel').destroy();
           banner.removeClass('owl-carousel').destroy();
        }
     }
     $(document).ready(mobile);
     $(window).resize(mobile);

demo url

演示网址

回答by Sreenadh Tg

You are applying the destroy function to an object which is undefined.You can try something like this.

您正在将 destroy 函数应用于未定义的对象。您可以尝试这样的操作。

if(typeof banner.data('owlCarousel') != 'undefined') {
    banner.data('owlCarousel').destroy();
    banner.removeClass('owl-carousel');
}

回答by David Reid

If this is your full code and you aren't just condensing it for easy reading, you are missing off the closing }for the mobile function.

如果这是您的完整代码并且您不只是为了便于阅读而将其压缩,那么您就错过}了移动功能的关闭。

It should be:

它应该是:

function mobile() {

  var checkWidth = $(window).width();
  var banner = $("#ndmv-banner-intro");

  if(checkWidth >980){

    banner.owlCarousel({
    singleItem:true,
    autoPlay:false,
    dragBeforeAnimFinish : true
   });

    }else{
       banner.data('owlCarousel').destroy();
       banner.removeClass('owl-carousel').destroy();
    }
}
$(document).ready(mobile);
$(window).resize(mobile);