jQuery 选择器问题?
我正在使用悬停功能将鼠标悬停在菜单中。我使用它的类选择了一个特定元素。要仅更改该元素而不更改该类的所有元素,我可以使用“this”。但我想更改该类元素内的 h3 标签。再次是我悬停过的元素,而不是所有具有该类名称的元素。
我尝试使用 > 'this' 之后的元素,但它不起作用。
我该怎么做?我希望我已经解释得足够好了。我希望你能理解使用代码。
$('.slide').hover(
function(){
$(this>'h3').animate({
height: '100%'
});
},
function(){
$(this>'h3').animate({
height: '25px'
});
}
);
所有答案均表示赞赏。 谢谢
I am using the hover function for mouse over in a menu. I have selected a particular element using it's class. To make changes to only that element and not all the elements with that class I can use 'this'. But I wanna make changes to the h3 tag inside that class element. Again the one I have hovered on and not all elements with that class name.
I tried using the > element after 'this' but it doesn't work.
How do I do that? I hope I have explained well enough. I hope you understand using the code.
$('.slide').hover(
function(){
$(this>'h3').animate({
height: '100%'
});
},
function(){
$(this>'h3').animate({
height: '25px'
});
}
);
All answers appreciated.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您使用
.find()
来获取<
元素。this
内的 h3>如果
是直接子元素,则使用
.children()
:You use
.find()
to get the<h3>
element insidethis
.If the
<h3>
is a direct child, it is a little more efficient to use.children()
:试试这个:
Try this:
使用:
或:
Use:
Or:
尝试使用
$(this).find("h3")
而不是$(this>'h3')
。Try
$(this).find("h3")
instead of$(this>'h3')
.您应该使用 $(this).find('h3') ,
或者如果您只想将 h3:s 直接放在 .slide 下方,则可以使用
You should use $(this).find('h3') as in
or if you only want the h3:s directly underneath .slide you can use
以上所有答案都是正确的。选择器不起作用的原因是普通的“this”关键字指向实际的 DOM 元素。您必须将“this”单独包装在 jQuery 对象中,如下所示:$(this),然后才能对其使用任何 jQuery 方法。
All of the above answers are correct. The reason why your selector didn't work is that the plain 'this' keyword points to the actual DOM element. You have to wrap 'this' in a jQuery object by itself like so: $(this) before you can use any jQuery methods on it.