简体   繁体   English

适用于兄弟姐妹/儿童/家长的JQuery选择器

[英]JQuery Selectors for siblings/children/parent

So I feel like my title is almost ambiguous but the truth is I'm not even sure how to clarify...hence the question. 所以我觉得我的头衔几乎是模棱两可的,但事实是我甚至不确定如何澄清...因此这个问题。

Html HTML

<tr>  
  <div class="thisis(this)inmyexample"></div>
  <div class="stuff">
    <div class="foo"></div>
  </div>
</tr>

My question is on the difference in selectors. 我的问题是选择器的不同之处。 Let's assume that .stuff is a sibling of $(this) If I use this: 我们假设.stuff$(this)的兄弟如果我使用这个:

$(this).closest('tr').find('.stuff .foo')

Now, I realize this isn't efficient coding but it's just as an easy example. 现在,我意识到这不是有效的编码,但它只是一个简单的例子。 With it, I will find all classes of .foo that are children of .stuff . 有了它,我会发现.foo所有类都是.stuff孩子。

If I use this: 如果我用这个:

$(this).siblings('.stuff .foo')

I won't find anything. 我找不到任何东西。 This is my inexperience with jquery talking, probably, but why wouldn't that kind of selector (what do you call a selector that uses a space or has both together, like '.stuff.foo' ?) work with something like .siblings? 这可能是我对jquery说话的经验不足,但是为什么不会那种选择器(你称之为使用空格的选择器还是两者一起,如'.stuff.foo' ?)与.siblings之类的东西一起工作? My expectation is that it would find all siblings with the class of .stuff and then find all children of .stuff with a class of .foo . 我的期望是,它会找到该类的所有兄弟姐妹.stuff ,然后找到的所有儿童.stuff一类的.foo Is it not working because siblings/children/parent is looking for just the sibling/parent/child and won't look past that? 它不起作用,因为兄弟姐妹/孩子/父母正在寻找兄弟姐妹/父母/孩子而不会过去吗?

I know I can use this: 我知道我可以用这个:

$(this).siblings('.stuff').children('.foo')

and I achieve my goal. 我实现了我的目标。 I'm just curious why my above selector (if that's what it's properly called) wouldn't work. 我只是好奇为什么我的上面的选择器(如果那是它被正确调用的那样)不起作用。

Thanks in advance! 提前致谢!

你回答了自己的问题,你找不到任何与$(this).siblings('。stuff .foo')的东西,因为.foo不是$(这个)的兄弟,它有点像'孙子'。选择器,它会查找.foo兄弟姐妹,它们有.stuff父母,而你有$(这个),.stuff和.foo的兄弟,.stuff的孩子,而不是$(这个)的兄弟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM