通过悬停选择 jquery 子项

发布于 2024-07-28 21:00:17 字数 229 浏览 2 评论 0原文

我有以下(损坏的)代码:

 $(".old_post").hover(function(){
    $(this > ".post_right_nav").show();

post _ right _ nav 是一个 div(包含其他 div),其中包含一些控件供用户按下。 我只想当用户将鼠标悬停在帖子上时显示这些控件。 如何正确选择每个帖子的子元素?

I have the following (broken) code:

 $(".old_post").hover(function(){
    $(this > ".post_right_nav").show();

post _ right _ nav is a div(containing other divs) that holds some controls for the user to press.
I'd like to only show these controls when the user is hovering over a post.
How can I properly select the child element of every post?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜巴黎 2024-08-04 21:00:29

您可以使用上下文,如下所示:在 this 上下文中搜索具有 class='post-right-nav' 的元素

$(".old_post").hover(function(){
    $(".post_right_nav", this).show();
...

这将获得所有后代,如果你只想要孩子,你可以执行以下操作

$(".old_post").hover(function(){
    $(this).children(".post_right_nav").show();
...

我找到了一篇快速文章,介绍了 jQuery 选择器中上下文的使用

http://beardscratchers.com/journal/jquery-its-all-about-context

You can use context, the following is saying: Search for the elements that have class='post-right-nav' within the context of this

$(".old_post").hover(function(){
    $(".post_right_nav", this).show();
...

That will get you all descendants, if you want just the children you could do the following

$(".old_post").hover(function(){
    $(this).children(".post_right_nav").show();
...

I found a quick article that goes over the use of context in the jQuery selector

http://beardscratchers.com/journal/jquery-its-all-about-context

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文