Owl Carousel 返回错误

Ric*_*ues 4 javascript jquery owl-carousel owl-carousel-2

我正在尝试在我的网站上安装 Owl Carousel 2,这是第一次使用它,并且按照所有说明操作我仍然遇到一些问题。

导入所有文件后:

<head>
<meta charset="utf-8">
<title>The badass man alive</title>
<!-- CSS -->
<link rel="stylesheet" href="assets/vendors/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="assets/vendors/owl-carousel/assets/owl.carousel.css">
<!-- Javascript -->
<script src="assets/vendors/jQuery/jquery-3.1.1.min.js" type="text/javascript"></script>
<script src="assets/vendors/owl-carousel/owl.carousel.min.js" type="text/javascript"></script>
<script src="assets/js/script.js" type="text/javascript"></script>
Run Code Online (Sandbox Code Playgroud)

添加演示 javascript 代码:

$(function() {
  $('.owl-carousel').owlCarousel({
    loop:true,
    margin:10,
    nav:true,
    responsive:{
        0:{
            items:1
        },
        600:{
            items:3
        },
        1000:{
            items:5
        }
    }
  });
});
Run Code Online (Sandbox Code Playgroud)

并尝试加载示例文件:

<div class="owl-carousel">
   <div class="item"><h4>1</h4></div>
   <div class="item"><h4>2</h4></div>
   <div class="item"><h4>3</h4></div>
   <div class="item"><h4>4</h4></div>
   <div class="item"><h4>5</h4></div>
   <div class="item"><h4>6</h4></div>
</div>
Run Code Online (Sandbox Code Playgroud)

控制台返回这个:

jQuery.Deferred exception: a(...).find(...).andSelf is not a function TypeError: a(...).find(...).andSelf is not a function
    at c.<anonymous> (file:///.../assets/vendors/owl-carousel/owl.carousel.min.js:2:7592)
    at HTMLDivElement.e (file:///.../assets/vendors/jQuery/jquery-3.1.1.min.js:2:3655)
    at HTMLDivElement.dispatch (file:///.../assets/vendors/jQuery/jquery-3.1.1.min.js:3:10315)
    at HTMLDivElement.q.handle (file:///.../assets/vendors/jQuery/jquery-3.1.1.min.js:3:8342)
    at Object.trigger (file:///.../assets/vendors/jQuery/jquery-3.1.1.min.js:4:5808)
    at HTMLDivElement.<anonymous> (file:///.../assets/vendors/jQuery/jquery-3.1.1.min.js:4:6318)
    at Function.each (file:///.../assets/vendors/jQuery/jquery-3.1.1.min.js:2:2815)
    at r.fn.init.each (file:///.../assets/vendors/jQuery/jquery-3.1.1.min.js:2:1003)
    at r.fn.init.trigger (file:///.../assets/vendors/jQuery/jquery-3.1.1.min.js:4:6294)
    at e.trigger (file:///.../assets/vendors/owl-carousel/owl.carousel.min.js:1:22366) undefined
Uncaught TypeError: a(...).find(...).andSelf is not a function(…)
Run Code Online (Sandbox Code Playgroud)

我不知道为什么它会返回这个错误。

小智 8

问题在于旧版本的 jQuery 中没有 andSelf 函数。尝试将以下内容添加到您的 .js 文件中

$.fn.andSelf = function() {
  return this.addBack.apply(this, arguments);
}
Run Code Online (Sandbox Code Playgroud)


Shy*_*C.S 5

.andSelf() 在 jQuery 1.8 中已弃用,并在 jQuery 3.0 中删除。从 jQuery 1.8 开始,应该使用 .addBack() 代替。

因此,为了快速而肮脏的修复,在owl.caorusel.min.js文件中,只需将单词“ andSelf ”替换为“ addBack ”,它就会起作用。