2014-06-22 60 views
0

我在同一頁面中使用了3個引導傳送帶。在第三個輪播中,我添加了一個文本以獨立顯示幻燈片圖像。我的第三個旋轉木馬酷似this -同一頁上的多個傳送帶

這是jQuery的我用我的旋轉木馬

// Set carousel options 
    $('#homepage-feature').carousel({ 
     interval: 8000 // 8 seconds vs. default 5 
    }); 

    // Set carousel options 
    $('#timetable-feature').carousel({ 
     interval: 10000 // 4 seconds vs. default 5 
    }); 


$('#image-gallery-carousel').carousel({ 
    interval: 5000 
}); 

$('#carousel-text').html($('#slide-content-0').html()); 

// When the carousel slides, auto update the text 
$('#image-gallery-carousel').on('slid.bs.carousel', function (e) { 
    var id = $('.item.active').data('slide-number'); 
    //alert(id); 
    $('#carousel-text').html($('#slide-content-'+id).html()); 
}); 

所有轉盤都工作,但在第三輪播的文本沒有工作。如果我只添加第三個輪播,那麼它正在工作。我的問題是爲什麼它不與其他兩個工作?

回答

2

當你有第3個旋轉木馬,我懷疑你的目標錯誤.item-active(第一傳送帶之一)

嘗試

// When the carousel slides, auto update the text 
$('#image-gallery-carousel').on('slid.bs.carousel', function (e) { 
    var id = $(this).find('.item.active').data('slide-number'); 
    //alert(id); 
    $('#carousel-text').html($('#slide-content-'+id).html()); 
}); 
+0

是。它的工作。其實發生了什麼? – TNK

+0

每個旋轉木馬都有一個「.item.active」類的孩子。當你調用$(「。item.active」)時,jQuery返回類「.item-active」的所有元素。然後,當您調用var id = $(...)。data(「slide-number」)時,該變量僅獲取第一個變量(請參閱http://jsfiddle.net/petetnt/U73bJ/)。在我的代碼中,你使用id#image-gallery-carousel(它在函數內部是$(this))的旋轉木馬,並通過http://api.jquery.com/find找到具有「.item-active」的元素/ –