CSS Selectors - If child class = ? then select the

2019-09-02 06:02发布

问题:

Possible Duplicate:
Is there a CSS parent selector?

Say I have the following CSS:

<div class="div1">
    <div class="div2">
        <div class="div3">
            <ul class="div4">
                <li class="div5">

                </li>
            </ul>
        </div>
    </div>
</div>

Is there a way to target, if li.div5 then target .div1 so like change the background of .div1 if li.div5 ?

回答1:

if($('li.div5').length) {
    $(this).parents('.div1')......
}


回答2:

In pure CSS - No, I'm afraid there is no parent selector.



回答3:

no, you can't with css only . there's not such selector working as an ancestor selector



回答4:

CSS cascades, so it only allows selection of children. There are no ascension selectors unless you implement something like jQuery and do it through javascript.