开发者

Selecting adjacent and previous siblings in CSS

开发者 https://www.devze.com 2023-02-25 13:35 出处:网络
Say for instanc开发者_开发知识库e I have the following HTML <ul> <li><a href=\"a\">A</a></li>

Say for instanc开发者_开发知识库e 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>

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.

Thanks


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


There is no selector that does that even in the CSS3 specification; you will either have to be creative in assigning classes to the elements and using other selectors, or go with jQuery.

0

精彩评论

暂无评论...
验证码 换一张
取 消