Html CSS:选择上一个兄弟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21237143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CSS: select previous sibling
提问by Hauke
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;
}
I already tried ul li + li:hover
but it doesn't work.
Can't belief there is no tricky hack that I could use.
我已经试过了,ul li + li:hover
但它不起作用。无法相信我可以使用任何棘手的黑客。
Sure I can do this easily through Javascript but I don't like using JS where I can get the same result with CSS.
当然,我可以通过 Javascript 轻松地做到这一点,但我不喜欢使用 JS 来获得与 CSS 相同的结果。
回答by arandompenguin
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 来做到这一点,如果你真的必须选择前一个兄弟,你将不得不求助于 Javascript 之类的。