类选择器中的 jQuery 类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3767512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery class within class selector
提问by fearofawhackplanet
<div class="outer">
<div class="inner"></div>
</div>
how do I find the inner div here?
我如何在这里找到内部 div?
$container.find('.outer .inner')
$container.find('.outer .inner')
is just going to look for a div with class="outer inner"
, is that correct?
只是要寻找一个带有 的 div class="outer inner"
,对吗?
so I tried
所以我试过了
$container.find('.outer > .inner')
$container.find('.outer > .inner')
but that doesn't seem to be working.
但这似乎不起作用。
Edit:
编辑:
I know its easy to find with something like
我知道用类似的东西很容易找到
$container.find('.outer').find('.inner')
$container.find('.outer').find('.inner')
but I'm looking for the kind of single selector syntax which reads better imho.
但我正在寻找那种读起来更好 imho 的单一选择器语法。
回答by Andy E
is just going to look for a div with class="outer inner", is that correct?
只是要寻找一个 class="outer inner" 的 div,对吗?
No, '.outer .inner'
will look for all elements with the .innerclass that also have an element with the .outerclass as an ancestor. '.outer.inner'
(no space) would give the results you're thinking of.
不,'.outer .inner'
将查找具有.inner类的所有元素,这些元素也具有以.outer类作为祖先的元素。 '.outer.inner'
(没有空格)会给出你想要的结果。
'.outer > .inner'
will look for immediate children of an element with the .outerclass for elements with the .innerclass.
'.outer > .inner'
将寻找与元素的直接子.outer与要素类.inner类。
Both '.outer .inner'
and '.outer > .inner'
should work for your example, although the selectors are fundamentally different and you should be wary of this.
双方'.outer .inner'
并'.outer > .inner'
应适用于你的榜样,虽然选择是根本不同的,你应该警惕这一点。
回答by Sarfraz
For this html:
对于这个 html:
<div class="outer">
<div class="inner"></div>
</div>
This selector should work:
这个选择器应该工作:
$('.outer > .inner')