将css从类收集到数组中,使用jQuery提供给另一个类

时间:2013-08-15 02:04:59

标签: jquery css

我有两组元素,两者都有相同数量的元素。我想知道如何从第一组中收集宽度(宽度是可变的),然后按照收集的顺序将其提供给第二组。

<div class="a">This element might have a width of 100px</div>
<div class="a">..while this one might be 50px</div>
<div class="a">..and this one is maybe 75px</div>

收集到数组[100,50,75],然后将其传递给另一组元素。

<div class="b">give me the 100px width</div>
<div class="b">and me the 50px width</div>
<div class="b">and me the 75px width</div>

1 个答案:

答案 0 :(得分:5)

这样的事情可以解决问题

$('.a').each(function(index) {
    var thisWidth = $(this).width();
    $('.b').eq(index).width(thisWidth);
})

DEMO