簡體   English   中英

jQuery-遍歷跨度,從數組分配寬度

[英]Jquery - Cycle Through Spans, Assigning Width From Array

有沒有一種方法可以在跨度之間循環並為每個值分配一個數組值。

例如:

我有一個包含值[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 () 
});

根據您的評論,我認為您的意思是:

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()函數的index參數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM