2016-11-28 87 views
0

每個元件的問題是,我想改變的innerHTML的屬性格式對於在列表中的每個名稱和動畫處理(淡入,延遲對於x秒,淡出)jQuery的顯示/隱藏在列表

<div id="welcomeBox">Welcome SOMETHING</div> 

var list = ["George","Bob","Tom"]; 

$.each(list, function() 
{ 
    $("#welcomeBox") 
     .eq(0) 
     .text('Welcome' + this) 
     .fadeToggle(1500) 
     .delay(5000) 
     .fadeToggle(1500); 
}); 

隨着代碼以上我只是得到3x歡迎湯姆消息。

+2

的ID應該是唯一的,所以'(0)'不應該需要蜜蜂.EQ。 –

+0

thx在評論,但這不是在那裏的問題上的答案 – TranceFusion

+2

我沒有意識到評論部分是張貼答案。我的錯。 –

回答

2
var list=["George","Bob","Tom"]; 

// recursive closure to iterate thru list 
(function recurse(index){ 
    // on fade use the callback to fade out 
    $("#welcomeBox").text('Welcome ' + list[index]).fadeIn(1500, function(){ 
     $("#welcomeBox").fadeOut(1500, function(){ 
      // after fade out, call the function again with the next index 
      recurse(undefined !== list[index+1] ? index+1 : 0); 
     }); 
    }); 
})(0); 

https://jsfiddle.net/6qo0L6mr/