2013-08-26 45 views
0

我試圖得到這個在實際環境中的工作:http://jsfiddle.net/LREwC/獲取淡出過渡工作在實時環境

這裏的測試HTML頁我設置:

<head> 
<title>1</title> 
<link href="/style.css" rel="stylesheet" type="text/css" /> 
<script type="text/javascript" src="/js/jquery-1.8.3.js"></script> 
</head> 
<body> 
<script type="text/javascript"> 
(function() { 

    var quotes = $(".quotes"); 
    var quoteIndex = -1; 

    function showNextQuote() { 
     ++quoteIndex; 
     quotes.eq(quoteIndex % quotes.length) 
      .fadeIn(2000) 
      .delay(2000) 
      .fadeOut(2000, showNextQuote); 
    } 

    showNextQuote(); 

})(); 
</script> 
<p class="quotes">first quote</p> 
<p class="quotes">second quote</p> 
<p class="quotes">third quote</p> 
</body> 

出於某種原因,它是不在實時環境中工作,但它在JSfiddle中正常工作。我在發佈代碼時做錯了什麼?

CSS和JS腳本文件鏈接正確。我測試了現場鏈接,我只是縮短了它,所以它沒有鏈接到我正在工作的網站。

+0

你有錯誤信息嗎? – raam86

+0

檢查您的瀏覽器的開發人員工具控制檯選項卡中的錯誤..並確保鏈接到您的js文件是正確的 – bipen

+0

這是一個錯字?它應該是'$(function(){Handler for .ready()called。) });' – j08691

回答

0

你必須等待所有的DOM元素要準備好:

jQuery(function($) { 

    var quotes = $(".quotes"); 
    var quoteIndex = -1; 

    function showNextQuote() { 
     ++quoteIndex; 
     quotes.eq(quoteIndex % quotes.length) 
      .fadeIn(2000) 
      .delay(2000) 
      .fadeOut(2000, showNextQuote); 
    } 

    showNextQuote(); 

}); 

另一種辦法是給你的腳本移動到文檔的末尾。

+0

就是這樣!非常感謝。 – Derek