admin管理员组

文章数量:1389768

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>

and this is the part of the 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()

and i am getting Uncaught TypeError: Cannot read property 'cycle' of undefined on

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

this line.

I've just started to use js and if its a trivial question, sorry for that.

thanks advance.

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>

and this is the part of the 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()

and i am getting Uncaught TypeError: Cannot read property 'cycle' of undefined on

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

this line.

I've just started to use js and if its a trivial question, sorry for that.

thanks advance.

Share Improve this question edited Dec 5, 2014 at 9:59 Ankit Srivastava 2561 silver badge8 bronze badges asked Dec 5, 2014 at 9:25 mstf dnmzmstf dnmz 211 silver badge2 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

This is because the code you posted, uses an external library called "jQuery"

its a Javascript file with lots of extension methods and other goodies. you probably didnt link it to your page.

you need to link jquery before all other scripts:

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

or if you dont want to depend on external links, you can download this library to your website and use it locally. download jquery from HERE

You're assigning value to slideIndex instead of parison.
Instead of this.

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

use this:

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

本文标签: javascriptUncaught TypeError Cannot read property 39cycle39 of undefinedStack Overflow