为什么这么称呼伪类?
a:hover
为什么叫“伪类”呢?
与“类”的概念有什么相似之处吗?
a:hover
Why is it called a "pseudo-class"?
Are there any similarities with the concept of "class"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 CSS 术语中,类是一个以句号开头的选择器,例如,
它将以以下形式使用。
“类”的使用更多的是指“具有共同特征并与其他事物不同的事物的集合或类别”按种类或质量”,而不是借用面向对象的术语。
伪类“不完全是一个真实的类”,因为用户代理定义何时和/或多少内容符合资格(例如
:hover
、:active
等)。In CSS terms, a class is a selector that starts with a full stop, e.g.
It would be used in the form
This use of "class" is more in the sense "a set or category of things having a common characteristic and differentiated from others by kind or quality", rather than borrowing from OO terminology.
A pseudo class is "not quite a real one" as the user agent defines when and/or how much content qualifies (like
:hover
,:active
, etc).这是伪,因为你没有成功; 浏览器“创建”了它,并允许您对其进行设置以更改链接处于该状态时的外观。
It's pseudo because you didn't make it; the browser 'created' it and allows you to set it to change the look of the link when it's in that state.
来自 w3c CSS2 选择器规范:
基本上,伪类是您可以附加样式的东西,但您永远不会在 HTML 中自己打印它。 此外,根据用户与 UI 的交互,可以获取和丢失伪类。
From the w3c CSS2 selector spec:
So basically, a pseudo-class is something you can attach a style to, but you never print it out yourself in the HTML. Also, a pseudo-clas can be aquired and lost depending on user interaction with the UI.
随着 CSS2/3 允许更复杂的元素规则(例如 input[type=checkbox] 等),术语伪类似乎越来越过时。
然而,伪类是唯一(或多或少)的 CSS 标识符使用属性选择器等,大多数浏览器倾向于遵循页面加载时所有元素的状态,并且所做的任何更改都会被忽略,但是使用伪类时,它们实际上会在伪类发生变化时更改样式。 。
因此,考虑到该特定定义,它们是类,因为该规则适用于共享相同“状态”的任何元素,因此可以被视为“类”,但它是伪的,因为它不是“类” t 是一个真正的属性定义的类,并且因为在查看页面的任何给定时间,“类”可能为真,也可能不为真,
我认为,对于某些基于 UI 的伪类(我是这样)也很有趣。特别考虑
:hover
)在任何给定时间只有一个元素可以真正拥有该“类”,因此根据我上面的定义,它几乎更像是一个伪 id。With CSS2/3 allowing for more sophisticated element rules (things like input[type=checkbox] and the like, the term pseudo-class seems more and more dated.
However, pseudo-classes are the only CSS identifiers that (more or less) reliably change with user interactions. With attribute selectors and what not, most browsers tend to go with the state of all elements on page load and any changes made are ignored. But with pseudo-classes, they actually change the style when the pseudo-class becomes true (or untrue).
So with that specific definition in mind, they are classes because the rule applies to any elements that share the same "state" and thus can be considered of a "class", but it's pseudo because it isn't a true attribute-defined class and because the "class" may or may not be true at any given time the page is viewed.
It's also interesting to note, I think, that with certain UI-based pseudo-classes (I'm thinking specifically of
:hover
) only one element at any given time can really have that "class" so it's almost more of a pseudo-id, based on my above definition.我想认为它们被称为伪类,因为它们没有由作者明确定义,而是至少从作者甚至用户代理的角度来看是一种逻辑或心理构造。
遵循理解 HTML 文档时经常使用的家庭比喻,您可以想象一些元素会自己思考,比如我是第一个孩子,我是一个 div,也是第四个孩子(意味着我的位置也是偶数)...对于像悬停这样由用户引起的事情,我们可以想象,当 a 标签或任何元素悬停在其上时,它会立即分组到已触摸的事物中,并且将应用相应的样式......并且如果可以将鼠标悬停在上面同时存在两个元素,他们会同时认为我们已被触摸,因此属于该类(悬停在事物上),因此两者都具有相同的风格
I would like to think they are called pseudo classes because they werent explicitly defined by the author but rather are a sort of logical or mental construct at least from the perspective of the author or even the user agent.
following the family metaphor often employed in understanding HTML documents you can imagine some elements thinking to themselves things like well am the first-child, i'm a div and also the 4th child(meaning my position is also even)...and as for things like hovering that are user induced one can imagine that when the a tag or whatever element is hovered on that it instantly gets grouped among things that have been touched and the corresponding style will be applied...and if its possible to hover over two elements at the same time that they would simultaneously think to themeselves we have been touched and so belong to the class(hovered over things) and hence both take on the same styles