Apply style to parent if it has child with css

This question already has an answer here:

  • Is there a CSS parent selector? 26 answers

  • It's not possible with CSS3. There is a proposed CSS4 selector, $ , to do just that, which could look like this (Selecting the li element):

    ul $li ul.sub { ... }
    

    See the list of CSS4 Selectors here.

    As an alternative, with jQuery, a one-liner you could make use of would be this:

    $('ul li:has(ul.sub)').addClass('has_sub');
    

    You could then go ahead and style the li.has_sub in your CSS.

    链接地址: http://www.djcxy.com/p/22820.html

    上一篇: 有没有CSS“haschildren”选择器?

    下一篇: 将样式应用于父项,如果它具有带CSS的子项