MAO-EYE 2009-06-18 19:59 采纳率: 100%
浏览 552
已采纳

是否有 CSS 父选择器?

How do I select the <li> element that is a direct parent of the anchor element?

In example my CSS would be something like this:

li < a.active {
    property: value;
}

Obviously there are ways of doing this with JavaScript but I'm hoping that there is some sort of workaround that exists native to CSS Level 2.

The menu that I am trying to style is being spewed out by a CMS so I can't move the active element to the <li> element... (unless I theme the menu creation module which I'd rather not do).

Any ideas?

转载于:https://stackoverflow.com/questions/1014861/is-there-a-css-parent-selector

  • 写回答

26条回答 默认 最新

  • elliott.david 2009-06-18 20:16
    关注

    There is currently no way to select the parent of an element in CSS.

    If there was a way to do it, it would be in either of the current CSS selectors specs:

    In the meantime, you'll have to resort to JavaScript if you need to select a parent element.


    The Selectors Level 4 Working Draft includes a :has() pseudo-class that works the same as the jQuery implementation. As of 2018, this is still not supported by any browser.

    Using :has() the original question could be solved with this:

    li:has(> a.active) { /* styles to apply to the li tag */ }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(25条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序