2013-03-12 156 views
0
var $e = $('.class'); 
$e.animate({ something:1 }); 

爲什麼我不能從變量調用任何jquery函數?HTMLElementVariable.animate(...)替代方案?

我想單獨爲每個數組的元素設置動畫。我如何根據它們在數組中的位置爲$ e中的變量設置動畫。我知道我可以用這個:

for (...) { 
    $('.class')[i].animate(); 
} 

但我猜測它會執行較慢,特別是如果有很多元素。那麼還有其他方法嗎?

在此先感謝。

+0

你可以調用任何jQuery函數從一個變量。你什麼意思? – Fresheyeball 2013-03-12 00:38:50

回答

1

.animate()函數是一個jQuery函數,所以你不能在DOM元素上調用它(你在問題中提到的for循環實際上不會工作)。你可以,但是,使用.each()迭代一個匹配的元素,並單獨它們的動畫:

$('.class').each(function(index, element) { 
    $(element).animate(); // index is its position in the list 
});