2010-07-05 115 views
2

我有問題選擇div我想要在jQuery中動畫。我有這個HTMLjquery選擇器

<div id="perimg"> 
<a class="false" href="show_model.php?param=Addison"> 
<div id="name_container"><img id="slide" src="../../pictures/picture.jpg"><div id="name">Picture name</div></div> 
</a> 
</div> 

我使用該標記,因爲我想滑動懸停上的圖片的#名稱。我有每行3行和8行,我的jQuery只針對第一個圖像。

$('#slide').hover(function(){ 
$(this).parent().find('#name').animate({"bottom" : 0}, 800) 
}, function(){ 
$(this).parent().find('#name').animate({"bottom" : "-20px"}, 800) 
}); 

我甚至試過去$('#perimg')。children(),但這也沒有幫助。

+0

ID必須在文檔中是唯一的。如果你有多個具有相同ID的元素,那麼這可能不起作用。 – Gumbo 2010-07-05 07:17:43

+0

嗯,我明白了。讓我再試一次 – andrei 2010-07-05 07:18:40

回答

3

由於有多個圖像和div您想要動畫,使用類而不是id。另外你的代碼是錯誤的,試試這個:

HTML:

<div id="perimg"> 
<a class="false" href="show_model.php?param=Addison"> 
<div id="name_container"><img class="slide" src="../../pictures/picture.jpg"><div class="name">Picture name</div></div> 
</a> 
</div> 

的jQuery:

$('.slide').hover(function(){ 
    $(this).find('.name').animate({"bottom" : 0}, 800) 
    }, function(){ 
    $(this).find('.name').animate({"bottom" : "-20px"}, 800) 
}); 
+0

我知道id的確是獨一無二的,我不知道爲什麼我犯了這個錯誤。它現在有用,謝謝,請記住。 – andrei 2010-07-05 07:22:50

1

jQuery只會在按ID進行檢查時選擇第一個匹配元素,但在檢查類時它將選擇全部。你會發生什麼,因爲你正在尋找一個特定的ID,它只會得到第一個圖像。只要改變它使用類,它應該工作。