CSS3 选择器根据第一个子级选择父级

发布于 2024-10-14 00:46:34 字数 58 浏览 1 评论 0原文

我可以编写一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄人书 2024-10-21 00:46:34

不,不幸的是不是,因为您将根据其后代选择祖先。遗憾的是,这与级联样式表的“级联”背道而驰。不过,如果这个功能能够实现的话,我会非常感激。

尽管给定标签“jquery-selectors”,您可以在 jQuery 中使用:

$('li:has("> a:first-child")')...

用 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:

$('li:has("> a:first-child")')...

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文