未捕获的类型错误: 无法读取未定义的属性“循环”

Uncaught TypeError: Cannot read property 'cycle' of undefined

本文关键字:未定义 读取 属性 循环 类型 错误      更新时间:2023-09-26

HTML Code:

<ol class="carousel-indicators">
                <li data-target="#carousel-popular-devices" data-slide-to="0" class="pip"></li>             
                <li data-target="#carousel-popular-devices" data-slide-to="1" class="pip"></li>                 
                <li data-target="#carousel-popular-devices" data-slide-to="2" class="pip active"></li>                  
                <li class="all">
                        <a href="3" class="all">blabla</a>
                </li>   
</ol>

这是JavaScript的一部分:

var $this = $(this), href
  , $target = $($this.attr('data-target') || (href = $this.attr('href')) && href.replace(/.*(?=#[^'s]+$)/, '')) //strip for ie7
  , options = $.extend({}, $target.data(), $this.data())
  , slideIndex
$target.carousel(options)
if (slideIndex = $this.attr('data-slide-to')) {
  $target.data('carousel').pause().to(slideIndex).cycle()
}
e.preventDefault()

并且我收到未捕获的类型错误:无法读取未定义的属性"循环"

$target.data('carousel').pause().to(slideIndex).cycle()

这一行。

我刚刚开始使用js,如果这是一个微不足道的问题,很抱歉。

谢谢提前。

这是因为您发布的代码使用了一个名为"jQuery"的外部库。

这是一个Javascript文件,有很多扩展方法和其他好东西。 您可能没有将其链接到您的页面。

您需要在所有其他脚本之前链接 jQuery:

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>

或者,如果您不想依赖外部链接,则可以将此库下载到您的网站并在本地使用。 从这里下载jQuery

您正在为幻灯片索引分配值而不是比较。
取而代之的是这个。

if (slideIndex = $this.attr('data-slide-to')) {
    $target.data('carousel').pause().to(slideIndex).cycle()
} 

使用这个:

if (slideIndex == $this.attr('data-slide-to')) {
    $target.data('carousel').pause().to(slideIndex).cycle()
}