有没有办法根据类中子元素的类来选择父元素?与我相关的示例是关于 http://drupal.org 的一个不错的菜单插件的 HTML 输出。 .输出呈现如下:
<ul class="menu">
<li>
<a class="active">Active Page</a>
</li>
<li>
<a>Some Other Page</a>
</li>
</ul>
我的问题是是否可以将样式应用于包含带有事件类的 anchor 的列表项。显然,我更希望将列表项标记为事件的,但我无法控制生成的代码。我可以使用 javascript 执行此类操作(我想到了 JQuery),但我想知道是否有一种方法可以使用 CSS 选择器来执行此操作。
明确一点,我想将样式应用到列表项,而不是 anchor 。
According to Wikipedia :
Selectors are unable to ascend
CSS offers no way to select a parent or ancestor of element that satisfies certain criteria. A more advanced selector scheme (such as XPath) would enable more sophisticated stylesheets. However, the major reasons for the CSS Working Group rejecting proposals for parent selectors are related to browser performance and incremental rendering issues.
对于将来搜索 SO 的任何人来说,这也可能被称为祖先选择器。
更新:
Selectors Level 4 Spec允许您选择选择的哪一部分是主题:
The subject of the selector can be explicitly identified by prepending
a dollar sign ($) to one of the compound selectors in a selector.
Although the element structure that the selector represents is the
same with or without the dollar sign, indicating the subject in this
way can change which compound selector represents the subject in that
structure.
Example 1:
For example, the following selector represents a list item LI unique child of
an ordered list OL:
OL > LI:only-child
However the following one represents an ordered list OL having a unique child,
that child being a LI:
$OL > LI:only-child
The structures represented by these two selectors are the same,
but the subjects of the selectors are not.
虽然这在任何浏览器中都不可用(目前是 2011 年 11 月),也不能作为 jQuery 中的选择器。