0

I'm just starting to learn Jquery, and have done a few tutorials, frustratingly, this one is one of the most basic I've done, and I can't seem to get it working.

This is the tutorial I'm following:- http://jonraasch.com/blog/a-simple-jquery-slideshow

When I start the page up, and run debug in Chrome and firebug in firefox I get the error:- "Uncaught ReferenceError: slideSwitch is not defined"

This is my code:-

$(document).ready(function(){
   function slideSwitch(){
       var $active = $('#slideshow IMG.active');
       var $next = $active.next();

       $active.addClass('last-active');

       $next.css({opacity: 0.0})
         .addClass('active')
         .animate({opacity:1.0}, 1000, function() {
              $active.removeClass('active last-active');
         });
  }
  $(function (){
      setInterval( "slideSwitch()", 5000 );
  });
});

As far as I can tell, I've defined the function slideSwitch correctly, and no one else in the comments box has had this problem. I'm sure its something really simple I'm doing wrong.

4

1 回答 1

2

您的slideSwitch函数未在全局范围内定义(仅在该.ready()hadler 内部),当您将字符串传递到setInterval()它正在查找的位置时,请执行以下操作:

$(function(){
   function slideSwitch(){
       var $active = $('#slideshow IMG.active');
       var $next = $active.next();

       $active.addClass('last-active');

       $next.css({opacity: 0.0})
         .addClass('active')
         .animate({opacity:1.0}, 1000, function() {
              $active.removeClass('active last-active');
         });
  }
  setInterval(slideSwitch, 5000 );
});

作为记录,这仍然有效(在你拥有它的地方,因为它在父范围内):

$(function (){
  setInterval(slideSwitch, 5000);
});

但是......你已经在一个document.ready处理程序中,所以这里不需要包装器。

于 2010-11-09T13:40:15.020 回答