2011-02-08 86 views
1

我有以下標記:jQuery選擇需要

<div class="thumb"> 
    <img src="thumb.jpg"/> 
    <p>Some text here</p> 
</div> 

基本上,我需要一個選擇,這樣,當拇指DIV ID上空盤旋,我可以動畫P孩。

我已經試過這樣:

$('.thumb').hover(function() { 
    $(this > "p")animate({ "top":"35px" }); 
}, function() { 
    $(this > "p").animate({ "top":"115px" }); 
}); 

不出於某種原因,雖然工作。

感謝

回答

4

this是一個對象,而不是字符串,所以你不能只是把它變成一個選擇。你想使用.find()方法來代替:

$(this).find("p").animate({"top": "35px"}); 

對應於選擇".thumb p"。要獲得與".thumb > p"相同的行爲,可以使用.children("p")而不是.find("p"),但這裏的區別並不重要。

這也是非常重要的,你讓沒有錯別字,如缺少.animate;)

1

可能:

 
$('.thumb').hover(function() { 
    $(this).find('p').animate({ "top":"35px" }); 
}, function() { 
    $(this).find('p').animate({ "top":"115px" }); 
}); 
+0

你怎麼改變找到孩子?這種方式應該是他想要看他的選擇者。 – Alxandr 2011-02-08 11:35:25

+0

那麼,孩子們()在閱讀時更直觀,但發現速度非常快;) – MatejB 2011-02-08 11:41:59

0

這不起作用:

$(this > "p") 

試試這個:

$(this).find('p') 
2

更改爲:

$('>p', this).animate({.... 

此構造中提取「這」第一級的P元素,這是你所試圖做的。你不需要使用find()或children()等等來獲得P標籤的第一級兒童。我認爲這個結構更簡單,更直觀。

P.S.你也有一個'。'在你的第一個'動畫'之前失蹤;