有这个的CSS选择器吗?
我在页面上有一两个 div,其中有一个/多个 foo 类。我只需要选择第二个,这意味着如果页面上只有一个,则不要选择它。
我尝试了 div.foo:not(:first)
和 div.foo:nth-child(2)
,但两者似乎都不起作用。
有什么想法吗?
I have one or two divs on the page with a class/classes of foo. I need to select the second one only, that means if there's only one on the page don't select it.
I tried div.foo:not(:first)
and div.foo:nth-child(2)
and both don't seem to work.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有
:nth-of-class()
选择器,但如果两个div.foo
元素共享相同的父元素,则可以使用同级选择器之一,具体取决于关于第二个div.foo
是否紧接在第一个之后:如果可能有两个以上这样的
div
,您可能需要撤消上面的样式规则对任何后续元素使用其中之一:并且不用担心浏览器支持,这适用于 IE7+。
There isn't an
:nth-of-class()
selector, but if bothdiv.foo
elements share the same parent, you can use one of the sibling selectors depending on whether the seconddiv.foo
comes immediately after the first or not:If there are potentially more than two such
div
s, you may need to undo the styles in the above rule using one of these for any subsequent elements:And don't worry about browser support, this works in IE7+.
这取决于几件事。
你是在IE上测试的吗?
里面的元素是同一个父元素吗?
如果这些元素不在同一个父级中,我不确定它们是否可以使用这些特定的选择器,并且据我所知,它们目前在 IE 中不起作用(不包括同级选择器,不确定 IE9+) 。
It depends on a few things.
Are you testing on IE?
Are the elements inside the same parent?
If the elements aren't inside the same parent, I'm not sure they can use those particular selectors, and as far as I can remember, they don't currently work in IE (excluding sibling selectors, and not sure about IE9+).