CSS:选择上一个同级 [英] CSS: select previous sibling
问题描述
我有一个列表,想要选择我悬停的上一个和下一个
I've got a list and want to select the previous and next of the one I am hovering
<ul>
<li>Item1</li>
<li>Item1</li>
<li>Item1</li>
<li>Item1</li>
<li>Item1</li>
</ul>
我可以使用+选择器选择下一个同级。
I am able to select the next sibling with the "+" selector. But is there a possibility to get the previous?
ul li:hover + li {
color:red;
}
我已经尝试过 ul li + li:hover
但它不工作。
不能相信有没有棘手的黑客,我可以使用。
I already tried ul li + li:hover
but it doesn't work.
Can't belief there is no tricky hack that I could use.
当然我可以通过Javascript轻松地做到这一点,但我不喜欢使用JS我可以得到与CSS相同的结果。
Sure I can do this easily through Javascript but I don't like using JS where I can get the same result with CSS.
推荐答案
没有办法做到这一点与纯CSS,如果你真的必须选择以前的兄弟姐妹,你将不得不诉诸Javascript或类似的。
There's no way to do this with pure CSS, if you really must select the previous sibling, you're going to have to resort of Javascript or suchlike.
这篇关于CSS:选择上一个同级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!