CSS3 选择器根据第一个子级选择父级
我可以编写一些 CSS3 选择器来执行以下操作:
选择第一个子级是“A”的“LI”吗?
Is there some CSS3 selector that I can write to do the following:
Select the "LI" whose first child is an "A"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,不幸的是不是,因为您将根据其后代选择祖先。遗憾的是,这与级联样式表的“级联”背道而驰。不过,如果这个功能能够实现的话,我会非常感激。
尽管给定标签“jquery-selectors”,您可以在 jQuery 中使用:
用 JS Fiddle 验证演示。
CSS 3 短暂地有一个
:contains() 伪选择器
可能对此有用,但此后似乎已被删除。Edited to incorporate @patrick dw's correction (from
$('li:has("> a")')...
to the above example.Edited to update the demo URL to a demonstration of the corrected jQuery selector.
No, unfortunately not, because you'd be selecting an ancestor based on its descendant. This operates against the 'cascade' of the Cascading Style Sheets, sadly. Though it's a feature that I would much appreciate were it ever to be possible.
Though given the tag 'jquery-selectors' you could, in jQuery, use:
Verified with a JS Fiddle demo.
CSS 3 briefly had a
:contains() pseudo-selector
that might have been useful for this, but since appears to have been removed.Edited to incorporate @patrick dw's correction (from
$('li:has("> a")')...
to the above example.Edited to update the demo URL to a demonstration of the corrected jQuery selector.