2013-10-04 34 views
2

我在主頁中有一個圖像傳送帶。爲了使其我使用Jquerytools(滾動+導航)AJAX頁面更新後重新初始化jQuery插件

我觸發jQuery的初始化腳本,以這樣的方式

$(window).load(function(){ 
    $("#today-news-carousel").scrollable({ vertical: true, mousewheel: true }).navigator({ navi: '#today-news-navigator' }); 

}); 

這個旋轉木馬的內容可以通過AJAX調用更新。 在這個調用之後,我需要重新初始化這個傳送帶。 這使得AJAX調用的函數:

$(document).on('click', '.nav-highlight', function() { 

     var requestDateArray = $(this).attr('data-thedate').split('-'); 
     var d = new Date(); 
     var requestedDate = new Date(requestDateArray[0], (requestDateArray[1]-1), requestDateArray[2]); 
     var today = new Date(d.getFullYear(), d.getMonth(), d.getDate()); 
     if (requestedDate > today) { 
      return 
     }else { 
     $.ajax({ 
      type: "POST", 
      url: templateDir+'/highlight-news-navigator.php', 
      context: this, 
      dataType: "html", 
      data: { date: $(this).attr('data-thedate') }, 
      beforeSend: function(){ 
      }, 
      success: function(data) { 
      $('.today-news').fadeOut('fast', function(){ 
       $(this).empty().html(data).fadeIn('fast'); 
      }); 
      }, 
      complete: function(){ 
      $("#today-news-carousel").scrollable({ vertical: true, mousewheel: true }) 
      .navigator({ navi: '#today-news-navigator' }); 
      } 
     }); 

     } 

    }); 

在「完整」的回調函數我嘗試重新初始化插件,但我有以下錯誤控制檯:

TypeError: $(...).scrollable(...).navigator is not a function 
.navigator({ navi: '#today-news-navigator' }); 

我不明白爲什麼它正常工作時,我加載頁面,當我重新初始化它似乎找不到.navigator方法...

+3

是否有原因將重新初始化代碼放入'complete',而不是'success'?這將更適合'fadeIn()'回調。沒有承諾,但值得一試。 – Archer

+0

Thaaaaank你!!!我在這個問題上度過了我所有的下午!把它放在淡出回調工作正常!再次感謝你! –

+0

...或者代替'$ .ajax({})。done'? – davidkonrad

回答

1

感謝弓箭手的幫助,我找到了解決方案。 重新初始化插件的腳本必須位於fadeIn()回調中。 這裏的工作代碼:

$(document).on('click', '.nav-highlight', function() { 

     var requestDateArray = $(this).attr('data-thedate').split('-'); 
     var d = new Date(); 
     var requestedDate = new Date(requestDateArray[0], (requestDateArray[1]-1), requestDateArray[2]); 
     var today = new Date(d.getFullYear(), d.getMonth(), d.getDate()); 
     if (requestedDate > today) { 
      return 
     }else { 
     $.ajax({ 
      type: "POST", 
      url: templateDir+'/highlight-news-navigator.php', 
      context: this, 
      dataType: "html", 
      data: { date: $(this).attr('data-thedate') }, 
      beforeSend: function(){ 
      }, 
      success: function(data) { 
      $('.today-news').fadeOut('fast', function(){ 
       $(this).empty().html(data).fadeIn('fast', function(){ 
       $("#today-news-carousel").scrollable({ vertical: true, mousewheel: true }).navigator({ navi: '#today-news-navigator' }); 
       }); 
      }); 
      }, 
     }); 

     } 

    }); 
+0

這個SO討論使我朝着正確的方向 - 當用JQuery分離/重新附加元素時,必須在任何動畫完成點重新初始化任何插件。 – TWright

相關問題