在CSS中选择相邻和先前的同级 [英] Selecting adjacent and previous siblings in CSS

查看:59
本文介绍了在CSS中选择相邻和先前的同级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有以下HTML

Say for instance I have the following HTML

<ul>
  <li><a href="a">A</a></li>
  <li><a href="b">B</a></li>
  <li class="myclass"><a href="c">C</a></li>
  <li><a href="d">D</a></li>
  <li><a href="e">E</a></li>
</ul>

现在,我可以使用以下css .someclass + li {}来确定class ="myclass"的li的下一个兄弟姐妹.但是,如果我想对先前的li应用CSS规则,是否可以使用CSS而不是jQuery的$(#some-id").prevAll()或编写一些JavaScript来做到这一点?

Now I can detemine the next sibling to the li with the class="myclass" by using the following css .someclass + li {}. However if I wanted to apply a CSS rule to previous li, is it possible to do this using css rather than jQuery's $("#some-id").prevAll() or writing some JavaScript to find this?

我不太自信,因为我从未听说过此消息,而且我的搜索没有提供解决方案.

I'm not that confident as I've never heard of this and my searching hasn't provided a solution.

谢谢

推荐答案

不,在CSS中,您无法遍历" dom结构.您只能递归继承样式定义.

Nope, in CSS you can't 'traverse' the dom structure. You can only inherit style definitions recursively.

这篇关于在CSS中选择相邻和先前的同级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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