jQuery 多个parent() 调用

发布于 2024-10-06 21:32:45 字数 462 浏览 0 评论 0原文

我有这个 jQuery:

$(this).parent().parent().find(".license_tooltip").stop(true, true).fadeIn(200);

$ (this) 对象嵌套在两个 div 中,如下所示:

<div>
    <div>
        <a href="">$(this) object</a>
    </div>

    <div>
        <a href="">object to fade in</a>
    </div>
</div>

有人能给我指出正确的方向,使我的 jQuery 更加精简吗?上面给出的结构被复制多次,因此使用类和 ID 是不可能的。

I have this jQuery:

$(this).parent().parent().find(".license_tooltip").stop(true, true).fadeIn(200);

The $(this) object is nested within two divs like this:

<div>
    <div>
        <a href="">$(this) object</a>
    </div>

    <div>
        <a href="">object to fade in</a>
    </div>
</div>

Can someone point me in the right direction to making my jQuery more streamlined? The structure presented above is replicated multiple times, so using classes and IDs is impossible.

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

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

发布评论

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

评论(3

萌化 2024-10-13 21:32:45

您可以使用类(或任何其他可选属性)和 .closest() 声明您想要的父级,如下所示:

<div class="container">
    <div>
        <a href="">$(this) object</a>
    </div>

    <div>
        <a href="">object to fade in</a>
    </div>
</div>

对于脚本:

$(this).closest(".container").find(".license_tooltip").stop(true, true).fadeIn(200);

You can use a class (or any other selectable attribute) and .closest() to claim to the parent you want, like this:

<div class="container">
    <div>
        <a href="">$(this) object</a>
    </div>

    <div>
        <a href="">object to fade in</a>
    </div>
</div>

And for the script:

$(this).closest(".container").find(".license_tooltip").stop(true, true).fadeIn(200);
暗喜 2024-10-13 21:32:45

您可以使用 .parents( [选择器] ) 这里是一个链接

它将向上遍历不止一个父级。

You could use the .parents( [ selector ] ) here is a link

It will traverse more than one parent up.

一抹淡然 2024-10-13 21:32:45

使用父母()

$(this)
    .parents('selector for the parent you need to look in')
    .find(".license_tooltip")
    .stop(true, true)
    .fadeIn(200);

use parents()

$(this)
    .parents('selector for the parent you need to look in')
    .find(".license_tooltip")
    .stop(true, true)
    .fadeIn(200);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文