2013-03-30 32 views
0

有沒有一種方法可以遍歷跨度併爲每個數組賦值。Jquery - 循環跨度,從數組中分配寬度

例如:

我有一個包含的值[45,50,106]的數組。是否有一種方法可以在將鼠標懸停並應用跨度數組值之間循環。因此,遇到的第一個跨度的寬度爲45.第二個跨度的值是50,等等。

// Multi-Expanding Icon Version:  
var widths = []; 
$("ul#navigation-three").children('li').children('span:nth-child(2)').each(function(){ 
    widths.push($(this).width()); 
}); 

$('ul#navigation-three').hoverIntent(function() {  
    // Assign each span:nth-child(2) with the widths from the array in order 
}, 

function() 
}); 
+0

需要明確的是 - 在跨第一個懸停給它是第一個數組值,第二個懸停在一個跨度上(包括原始的任何跨度?)獲得第二個寬度,依此類推?當數組中只有3個值時,第四個跨度怎麼樣? – OpherV

+0

這樣的事情,但這不起作用,但顯示了這個想法好一點,我認爲: '我= 0; \t \t \t \t $( 「#UL導航三>利」)的孩子( '跨度:第n個孩子(2)')。每個(函數(){ \t \t \t $(本).animate( { 「寬度」:+寬度[I] + 「PX」},0); \t \t \t I = I ++; \t \t});' 因此,每個找到一個跨度時間:第n個孩子它使寬度數組中的下一個數字。 – Tenatious

回答

1

基於您的評論,我假定你的意思是這樣的:

var widths=[10,20,30]; 
$("ul#navigation-three > li").children('span:nth-child(2)').each(function(index){ 
     $(this).animate({"width":+widths[index]+"px"}, 0); 
}); 

我曾經給出的指標參數的each()功能

+0

美麗!正是我需要的。我當時並不太遠:P – Tenatious

+0

只是知道語法的問題:)很高興我能幫忙 – OpherV