使用 jQuery 选择器在类中查找类
中执行类似以下操作
我正在尝试在 jQuery $('.container').siblings('.outerClass > .innerClass')
:
<div class="container"></div>
<div class="outerClass">
<div class="innerClass">
find me!
</div>
</div>
我无法获取语法正确。
I'm trying to do something like the following in jQuery
$('.container').siblings('.outerClass > .innerClass')
where I'm looking for:
<div class="container"></div>
<div class="outerClass">
<div class="innerClass">
find me!
</div>
</div>
I can't get the syntax right.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
还有另一个(但这应该可行)(假设您想获取带有
innerClass
类的元素):And another one (but this should work) (assuming you want to get the element with class
innerClass
):忽略容器选择器并仅使用 or 是否存在问题
(如果您不想要求内部 div 成为直接子元素)
Is there a problem ommitting the container selector, and just using
or perhaps (if you don't want to require the inner div to be a direct child)
您也可以这样做来直接选择所需的元素
You can also do this to directly select the required elements
解释原因; <代码>.outerClass> .innerClass 是一个选择器,它选择具有类innerClass(而不是outerClass)的元素。要选择其中包含特定内容的元素,请使用
:has
选择器,该选择器将选择器作为参数。To explain why;
.outerClass > .innerClass
is a selector that selects an element with class innerClass, not the outerClass. To select an element that has something specific in it, you use the:has
selector, which takes a selector as an argument.