通过孩子设置父母风格 [英] Set parent style via child

查看:48
本文介绍了通过孩子设置父母风格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下< ul> :

<ul>
<li>Child</li>
<li><span class="active">Child</span></li>
<li>Child</li>
</ul>

我是否可以通过 active &span> 来更改< li> 的样式?

Is it possible for me to change the style of the <li> via the active <span>?

例如:

.active:parent(li) {
    background-color: red;
}

我已经可以使用jQuery实现此功能,但是它在屏幕上闪烁了一秒钟(等待DOM加载),我想避免这种情况.

I've been able to achieve this with jQuery, but it flickers on the screen for a split second (waiting for the DOM to load) and I want to avoid that.

推荐答案

在CSS中无法做到这一点.

There's no way to do this in CSS.

最好的方法是做类似的事情:

The best thing to do would be to do something like:

<ul class="has-active">
    <li>Child</li>
    <li><span class="active">Child</span></li>
    <li>Child</li>
</ul>

样式

ul.has-active  {
    ...
}

如果您正在寻找可以做父选择器的东西,那么看看 xpath .

If you are looking for something that can do parent selectors, then look at something like xpath.

这篇关于通过孩子设置父母风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆