css - 活跃 child 的 parent 的复杂 CSS 选择器

标签 css css-selectors

<分区>

有没有办法根据类中子元素的类来选择父元素?与我相关的示例是关于 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 中的选择器。

关于css - 活跃 child 的 parent 的复杂 CSS 选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45004/

相关文章:

html - 每个类组的最后一名

css - 是否有 CSS 父级选择器?

css - 在没有明确边距值的页面上居中嵌套 Div

html - (HTML/CSS) 如何在父元素中垂直居中网格

css - 跨越 ul 的宽度跨越 li 元素的填充

javascript - CSS 组合 :not with :last-of-type

jquery - 如何使用 [attribute=value] 选择器?

html - 如何向上移动各种大小的 'cells' ?

css - 如何使用 Skeleton Boilerplate 在 div 中居中图像

html - 定位选中输入的标签