TypeError: $(…).owlCarousel is not a function

前端 未结 8 842
青春惊慌失措
青春惊慌失措 2020-12-11 16:26

I am having troubles adding this carousel to my prestashop template.

It returns me the following error:

TypeError: $(...).owlCarousel is not a function navig

相关标签:
8条回答
  • 2020-12-11 16:30

    I got fixed by checking if the selector exist or not. As this was the issue on my website I had added code in footer for a single page but it was throwing error or other pages where that selector don't exist.

    $(document).ready(function() {
        var owl = $('.servicesCarosal');
                
        //console.log(owl);
    
        if (owl.length) { 
            owl.owlCarousel({
    
            });
        } 
    });
    
    0 讨论(0)
  • 2020-12-11 16:36

    The reason sometime html executed inline script before external script loaded perfectly. I get solution by this way . I just added defer attribute to my owl.carouselsource calling like ..

    <script defer src="plugins/OwlCarousel2.3/owl.carousel.min.js"></script>

    Documentation about defer attribute --> att_script_defer-link

    0 讨论(0)
  • 2020-12-11 16:37

    You will get this error if the jquery file is loading after the owl carousel file.

    (Make sure your reference to jquery is above the owl carousel reference js file)

    0 讨论(0)
  • 2020-12-11 16:39

    Try to use {literal} {/literal} tags. It's usually reccomanded to put javascript inside those tags in .tpl files (smarty) . Javascript might work without the tags but can sometimes return a error ( like in your case )

    BR's

    0 讨论(0)
  • 2020-12-11 16:44

    If JavaScript files loading is affected by some latency, you can check if function is defined before call it.

    Check with jQuery.isFunction

    if($.isFunction('owlCarousel')){
      $("#owl-demo").owlCarousel({
        navigation : true
      });
    }
    

    Check with JavaScript typeof operator

    if(typeof owlCarousel === 'function') { 
      $("#owl-demo").owlCarousel({
        navigation : true
      });
    }
    
    0 讨论(0)
  • 2020-12-11 16:47

    I had the same problem. Just add the js file right above your function

    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/owl.carousel.min.js"></script>
    <!--DO NOT ENTER ANY EXTERNAL LINK IN BETWEEN-->
    <script type="text/javascript">
    $(document).ready(function() {
        $('.owl-carousel').owlCarousel({
            loop: true,
        });
    });
    </script>
    
    0 讨论(0)
提交回复
热议问题