2010-11-09 83 views
0

我剛剛開始學習jquery,並做了一些教程,令人沮喪的是,這是我做過的最基本的一個,我可以「不像是會得到它的工作圖片庫斷開爲「slideSwitch is not defined」:-(

這是我下面的教程: - 。http://jonraasch.com/blog/a-simple-jquery-slideshow

當我開始向上翻頁,並運行在Chrome調試和Firefox中的Firebug我得到的錯誤: - 「Uncaught ReferenceError:slideSwitch is not defined

這是我的代碼: -

$(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); 
    }); 
}); 

據我所知,我已經正確定義了slideSwitch函數,並且在評論框中沒有其他人有這個問題。我確信它的事情很簡單,我做錯了。

+0

你確定你必須以這種方式調用setInterval嗎?從我看到的情況來看,最好使用'setInterval(slideSwitch,5000);'如下所述:https://developer.mozilla.org/en/DOM/Window.setInterval – 2010-11-09 13:28:21

+0

嘗試從slideSwitch中移除所有內容,然後只需在那裏添加一個警報。如果它起作用,那麼問題出現在函數內部。 – 2010-11-09 13:30:01

回答

2

slideSwitch功能沒有在全球範圍內定義的(僅適用於內部的.ready()哈德勒),當你傳遞一個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處理,所以re在這裏不需要包裝。

相關問題