CSS 伪类
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html> <head>
<title>How to Write a Good Report</title>
<style type="text/css">
div.weather strong:first-child {color:red;}
</style>
</head>
<body>
<div class="weather">
It's <strong>very</strong> hot and <strong>incredibly</strong> humid.
</div>
</body> </html>
为什么只有“very”是红色,而“incredible”不是红色,因为它们都是“div.weather Strong”指定的元素的第一个子元素?
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html> <head>
<title>How to Write a Good Report</title>
<style type="text/css">
div.weather strong:first-child {color:red;}
</style>
</head>
<body>
<div class="weather">
It's <strong>very</strong> hot and <strong>incredibly</strong> humid.
</div>
</body> </html>
Why only "very" is in color red while "incredibly" not since both of them are first child of the element specified by "div.weather strong"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
因为伪选择器不会做你认为它会做的事情。
在您所说的示例中,它选择元素
.weather
的第一个子元素,即元素。
所以只有第一个实例匹配。 我无法参考规范来支持这一点,因为我现在很懒(忙碌的一天......),但我脑子里有这样的愿景:
我知道,这不是最漂亮的 ascii 示例,但是我就是这么想的。
Because the psuedo-selector doesn't do what you think it does.
It selects, in your stated example, the first child of the element
.weather
that is a<strong>
element.So only the first instance matches. I can't back this up with reference to the spec, since I'm lazy right now (busy day...), but I have this sort of vision in my head:
It's not the prettiest ascii example, I know, but it's how I think of it.
我认为您的意思是
,这将仅选择子代(嵌套的第一层)而不是所有后代。
I think you've meant
That will select children only (first level of nesting) rather than all descendants.
first-child
表示“第一个子元素”,而不是“该元素的第一个子元素”。所以你的例子意味着“
strong
是div.weather
的第一个孩子”。例如,如果您想让 div 中所有
中的第一个单词显示为红色,则需要添加一些标记并执行如下操作:
first-child
means "an element that is the first child", not "the first child of this element."So your example means "the
strong
that is the first child ofdiv.weather
".If you want to make, say, the first word in all the
<strong>
s within the div come out red, you'll need to add some markup and do something like this:div.weather Strong:first-child 表示选择第一个强元素,即天气的子元素,而不是天气的所有子元素。 它只会匹配单个元素。
div.weather Strong 匹配所有作为weather 后代的强元素。
div.天气> 强匹配所有直接影响天气的强元素。
div.weather * Strong 匹配所有weahter 的孙子或更晚的强元素。
div.weather strong:first-child says to select the first strong element that is a child of weather, not all of the child elements of weather. It's only going to match a single element ever.
div.weather strong matches all of the strong elements that are descendants of weather.
div.weather > strong matches all of the strong elements that are direct children of weather.
div.weather * strong matches all of the strong elements that are grandchildren or later of weahter.
我认为你对“第一胎”的理解是错误的。 第一个子元素只是第一个元素。 请参阅 W3Schools 上的示例。
I think your understand of "first-child" is wrong. The first child is the the first element only. See the example at W3Schools.
两个
strong
元素都是div
的子元素,但只有第一个元素是第一个strong
子元素。 所以你只是误解了选择器。Both
strong
elements are a child of thediv
but only the first one is the firststrong
child. So you just misunderstood the selector.