我有以下标记:
<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" });
});
由于某种原因不起作用。
谢谢
最佳答案
this
是一个对象,而不是字符串,因此您不能将其放入选择器中。您想改用 .find()
方法:
$(this).find("p").animate({"top": "35px"});
对应于选择器“.thumb p”
。要获得与 ".thumb > p"
相同的行为,您可以使用 .children("p")
而不是 .find("p")
,但这里的区别并不显着。
还有一点非常重要,那就是不要出现拼写错误,例如 animate
之前缺少 .
;)
关于需要 jquery 选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4932482/