如何选择具有可见父级的类的所有元素?
这就是我所拥有的,但它似乎不起作用。
$('.parent-class:visible .my-class[state!="done"]')
我想选择其 .parent-class
元素可见的所有 .my-class
元素。
编辑1:此选择器是正确的。代码中的其他地方肯定存在错误。所以这个问题已经不再重要了。
编辑2:真正的问题是我的父类附加了内联样式“display:inline”,显然可见选择器不喜欢display:inline。我把它拿出来后它就开始工作了。
编辑 3: 未设置高度的内联元素。 jQuery 说任何高度为零的东西都被认为是不可见的,我想我的高度未知的元素默认为零。
This is what I have but it doesn't seem to be working..
$('.parent-class:visible .my-class[state!="done"]')
I want to select all .my-class
elements that their .parent-class
element is visible.
Edit 1: This selector is correct. There bug must be elsewhere in the code. So this question is no longer relevant.
Edit 2: The real problem was that my parent class had the inline style "display:inline" attached to it, apparently the visible selector doesn't like display:inline. After I took that out it started working.
Edit 3: An inline element with an unset height. jQuery says anything with a height of zero is considered invisible, I suppose my element with an unknown height was defaulted to zero.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你的选择器应该可以工作,而且它确实可以工作。在你的小提琴中,你没有包含 jQuery 库,所以它在那里不起作用!请参阅此更新的小提琴。
Your selector should work, and it does work. In your fiddle, you have not included the jQuery library, so it doesn't work there! See this updated fiddle.
如果你将 jsfiddle 中的框架更改为 jquery 而不是 mootools 它可以工作
if you change the framework in jsfiddle to jquery instead of mootools it works
您拥有它,因此父级的所有后代都被选中。 空间是“祖先运算符”。
如果您只想要直接子级,请使用 >操作员:
You have it so all descendents of the parent are selected. The space is the "ancestor operator".
If you want only the direct children, use the > operator:
尝试
这里是小提琴 http://jsfiddle.net/XAnqB/7/
try
here is the fiddle http://jsfiddle.net/XAnqB/7/