hurriedly% 2009-06-18 19:59 采纳率: 100%
浏览 1864
已采纳

是否有 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条回答 默认 最新

  • derek5. 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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?