2012-04-19 24 views
1

我正在使用Jquery UI ajax選項卡來顯示我網站上的內容。這些標籤工作得很好。在我的Ajax選項卡的每個選項卡上,我都有一個圖像旋轉器,它的效果並不好。在ajax窗口中使用jquery

圖像旋轉需要jquery正常工作。所有的jQuery像器插件文件似乎是工作的罰款,除了這個代碼...

  jQuery(document).ready(function($) { 


      var banner1 = $('#banner-rotator1').royalSlider({ 
       imageAlignCenter:true, 
       imageScaleMode: "fill", 

       hideArrowOnLastSlide:true, 
       slideSpacing:20, 

       autoScaleSlider: true, 
       autoScaleSliderWidth: 1150, 
       autoScaleSliderHeight: 400 
      }).data("royalSlider");    


      var banner2 = $('#banner-rotator2').royalSlider({ 
       imageAlignCenter:true, 
       imageScaleMode: "fill", 

       hideArrowOnLastSlide:true, 
       slideSpacing:20, 

       autoScaleSlider: true, 
       autoScaleSliderWidth: 1150, 
       autoScaleSliderHeight: 400 
      }).data("royalSlider"); 

      var banner3 = $('#banner-rotator3').royalSlider({ 
       imageAlignCenter:true, 
       imageScaleMode: "fill", 

       hideArrowOnLastSlide:true, 
       slideSpacing:20, 

       autoScaleSlider: true, 
       autoScaleSliderWidth: 1150, 
       autoScaleSliderHeight: 400 
      }).data("royalSlider"); 

      var banner4 = $('#banner-rotator4').royalSlider({ 
       imageAlignCenter:true, 
       imageScaleMode: "fill", 

       hideArrowOnLastSlide:true, 
       slideSpacing:20, 

       autoScaleSlider: true, 
       autoScaleSliderWidth: 1150, 
       autoScaleSliderHeight: 400 
      }).data("royalSlider"); 
     }); 

我知道代碼工作,因爲如果我剪切並粘貼到正常工作的AJAX頁面,但我可以當代碼被放置在「母版頁」中時,似乎沒有讓圖像旋轉器工作。有誰知道我做錯了什麼?

這裏是我的網頁http://dl.dropbox.com/u/14080718/Ajax-Test/Ajax-stripped.html

回答

1

這是因爲這段代碼在運行時的DOM準備好一個鏈接,即,母版頁DOM已準備就緒。您稍後加載您的ajax內容並且javascript代碼已經在加載ajax內容之前運行。你可以做的是將該代碼放入一個函數中。然後在你的AJAX加載上document.ready

的功能,例如

在你的母版

function code_to_run(){ 
    // your code here 
} 

在你的Ajax加載的內容

$(function(){ 
    code_to_run() // call the function in your masterpage 
}); 
+0

我想保留所有的jQuery我的ajax頁面。它看起來像很多移動瀏覽器很難從ajax頁面加載jquery。你知道一個方法來做到這一點嗎?也許是不同的電話或什麼? – 2012-04-19 18:03:14

+0

嗯,當然。在加載ajax頁面之後,您也可以在回調中執行此操作。 – 2012-04-19 18:09:12

+0

不完全確定你的意思。對不起,我很新。你介意發送一個例子嗎? – 2012-04-19 20:15:11