jQuery:如何获取父级的特定子级?

发布于 2024-08-24 09:28:59 字数 726 浏览 3 评论 0原文

举一个简化的例子,我在页面上多次重复以下块(它是动态生成的):

<div class="box">
   <div class="something1"></div>
   <div class="something2">
      <a class="mylink">My link</a>
   </div>
</div>

单击时,我可以通过以下方式到达链接的父级:

$(".mylink").click(function() {
   $(this).parents(".box").fadeOut("fast");
});

但是...我需要到达该特定父级的

基本上,有人可以告诉我如何在无法直接引用的情况下引用更高级别的兄弟姐妹吗?我们就称呼它为大哥吧。直接引用老大哥的类名会导致页面上该元素的每个实例淡出 - 这不是所需的效果。

我试过了:

parents(".box .something1") ... no luck.
parents(".box > .something1") ... no luck.
siblings() ... no luck.

有人吗?谢谢。

To give a simplified example, I've got the following block repeated on the page lots of times (it's dynamically generated):

<div class="box">
   <div class="something1"></div>
   <div class="something2">
      <a class="mylink">My link</a>
   </div>
</div>

When clicked, I can get to the parent of the link with:

$(".mylink").click(function() {
   $(this).parents(".box").fadeOut("fast");
});

However... I need to get to the <div class="something1"> of that particular parent.

Basically, can someone tell me how to refer to a higher-level sibling without being able to refer to it directly? Let's call it big brother. A direct reference to the big brother's class name would cause every instance of that element on the page to fade out - which is not the desired effect.

I've tried:

parents(".box .something1") ... no luck.
parents(".box > .something1") ... no luck.
siblings() ... no luck.

Anyone? Thanks.

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

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

发布评论

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

评论(5

紫竹語嫣☆ 2024-08-31 09:28:59

调用 .parents(".box .something1") 将返回与选择器 .box .something 匹配的所有父元素。换句话说,它将返回 .something1 且位于 .box 内部的父元素。

您需要获取最近父级的子级,如下所示:

$(this).closest('.box').children('.something1')

此代码调用 .closest 获取与选择器匹配的最内层父元素,然后在该父元素上调用 .children 来查找您要查找的叔叔。

Calling .parents(".box .something1") will return all parent elements that match the selector .box .something. In other words, it will return parent elements that are .something1 and are inside of .box.

You need to get the children of the closest parent, like this:

$(this).closest('.box').children('.something1')

This code calls .closest to get the innermost parent matching a selector, then calls .children on that parent element to find the uncle you're looking for.

北渚 2024-08-31 09:28:59
$(this).parent()

树遍历很有趣,

$(this).parent().siblings(".something1");

$(this).parent().prev(); // if you always want the parent's previous sibling

$(this).parents(".box").children(".something1");

还有更多方法,您可能会发现这些文档很有帮助。

$(this).parent()

Tree traversal is fun

$(this).parent().siblings(".something1");

$(this).parent().prev(); // if you always want the parent's previous sibling

$(this).parents(".box").children(".something1");

And much more ways, you might find these docs helpful.

三五鸿雁 2024-08-31 09:28:59

这将找到第一个具有 box 类的父类,然后找到第一个具有正则表达式匹配 something 的子类并获取 id。

$(".mylink").closest(".box").find('[class*="something"]').first().attr("id")

This will find the first parent with class box then find the first child class with regex matching something and get the id.

$(".mylink").closest(".box").find('[class*="something"]').first().attr("id")
哀由 2024-08-31 09:28:59

如果我正确理解您的问题,$(this).parents('.box').children('.something1') 这是您要找的吗?

If I understood your problem correctly, $(this).parents('.box').children('.something1') Is this what you are looking for?

天冷不及心凉 2024-08-31 09:28:59

您可以将 .each().children() 和括号内的选择器一起使用:

//Grab Each Instance of Box.
$(".box").each(function(i){

    //For Each Instance, grab a child called .something1. Fade It Out.
    $(this).children(".something1").fadeOut();
});

You could use .each() with .children() and a selector within the parenthesis:

//Grab Each Instance of Box.
$(".box").each(function(i){

    //For Each Instance, grab a child called .something1. Fade It Out.
    $(this).children(".something1").fadeOut();
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文