Html 如何结合 :first-child 和 :hover?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1800100/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:24:01  来源:igfitidea点击:

How to combine :first-child and :hover?

htmlcss

提问by Diodeus - James MacFarlane

I have an unordered list I'm using for a menu. Each item has a background image and a :hover image. The background image on the first element is different that the rest, so I use the following to style it, which works fine:

我有一个用于菜单的无序列表。每个项目都有一个背景图像和一个 :hover 图像。第一个元素上的背景图像与其他元素不同,因此我使用以下内容对其进行样式设置,效果很好:

#prodNavBar ul:last-child li:first-child {...}

Since I want a roll-over image on this element as well, I've tried adding :hover, like so:

由于我也想要在此元素上滚动图像,因此我尝试添加:hover,如下所示:

#prodNavBar ul:last-child li:first-child:hover {...}

...but this doesn't work. What's the syntax to combine :first-childand :hover?

...但这不起作用。组合:first-childand的语法是:hover什么?

采纳答案by stephenhay

Chaining :first-childand :hoveras you do here should work just fine. If you're using IE6, however, only the last pseudo-class in the chain will be recognized.

链接:first-child:hover你在这里做的应该工作得很好。但是,如果您使用的是 IE6,则只会识别链中的最后一个伪类。

In other words, you're doing it right.

换句话说,你做对了。

回答by Chetan Sastry

li:first-child:hovershould work. Which browser are you testing with? IE doesn't support last-child

li:first-child:hover应该管用。你用哪个浏览器测试?IE 不支持last-child

Hereis a sample test case.

是一个示例测试用例。