2012-07-17 16 views
1

我有jQuery的內容的頁面加載,在div之一將是一個滑塊如何在jQuery中修改HTML時刷新對象?

$('#slides').slides({ 
      preload: true, 
      preloadImage: 'img/loading.gif', 
      play: 6000, 
      pause: 2500, 
      hoverPause: true, 
      slideSpeed: 1000, 
      animationStart: function(current){ 
       $('.caption').animate({ 
        bottom:-35 
       },100); 
      }, 
      animationComplete: function(current){ 
       $('.caption').animate({ 
        bottom:0 
       },200); 
      }, 
      slidesLoaded: function() { 
       $('.caption').animate({ 
        bottom:0 
       },200); 
      } 
     }); 
    }); 

目前,HTML的結構是這樣的:

<div id"content"> 
    <div id="slider"></div> 
</div> 

我使用了我的頁面設置jQuery的,當用戶點擊一個鏈接,它會在該網頁的內容。如果用戶可以追溯到豪加載到#content

var homepage = $("#content").html(); 
$("#content").load("myContent.html"); 

我只是:

$("#content").html(homepage); 

但是然後$('#slides').slides();不再工作。

我該如何解決這個問題?

SlidesJS

回答

3

您可以使用​​的complete來檢查您的內容是否有#slider。如果有,請再次初始化您的slides插件。

var homepage = $('#content'), 
options = { 
    generateNextPrev: true, 
    play: 1000 
}; 
$('#slides').slides(options); 

// Button click or something 
$('#content').load('myContent.html', function(response, status, xhr) { 
    if ($(response).find('#slider').length > 0) { 
     $('#slides').slides(options); 
    } 
}); 

編輯:

我是在製造的jsfiddle證明,但的jsfiddle是具有服務器的問題,所以我做了一個演示codepen。 http://codepen.io/Vestride/pen/LicKd。調用插件再次爲我工作(雖然我只是用.html()替換內容來模擬AJAX請求)。

+0

我確信#slides會存在於「var主頁」中,我要問的是如何製作幻燈片();在我更換html之後再次工作,然後再次將相同的html(帶有#slides的那個)返回 – tom91136 2012-07-17 05:37:28

+0

@ Tom91136也確保您調用'$('#slides')。幻燈片(選項)'。如果這樣也行不通,請在調用'slides()之前保存'homepage'變量。我編輯了上面的示例。 – Vestride 2012-07-18 06:48:44

+0

謝謝,它終於在頭痛的日子裏工作了! – tom91136 2012-07-18 18:28:30

1

試試這個,在功能

認沽滑塊,

function SliderCode() 
{ 

$('#slides').slides({ 
      preload: true, 
      preloadImage: 'img/loading.gif', 
      play: 6000, 
      pause: 2500, 
      hoverPause: true, 
      slideSpeed: 1000, 
      animationStart: function(current){ 
       $('.caption').animate({ 
        bottom:-35 
       },100); 
      }, 
      animationComplete: function(current){ 
       $('.caption').animate({ 
        bottom:0 
       },200); 
      }, 
      slidesLoaded: function() { 
       $('.caption').animate({ 
        bottom:0 
       },200); 
      } 
     }); 
    }); 
} 

重新設置hompage callslider功能後,

$("#content").html(homepage); 
SliderCode(); 

我因此未測試的代碼。希望它能起作用。

+0

它有點作品,但它攪動滑塊的幻燈片 – tom91136 2012-07-17 05:28:10

+0

有沒有更好的方法來做到這一點?像摧毀.slides();第一?我怎麼做? – tom91136 2012-07-17 05:28:47

+0

或者你可以把滑塊放在iframe中。然後iframe中的頁面html – Matt 2012-07-17 05:32:37