:focus 和 :active 有什么区别?
:focus
和 :active
伪类有什么区别?
What is the difference between the :focus
and :active
pseudo-classes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
:focus
和 :active
伪类有什么区别?
What is the difference between the :focus
and :active
pseudo-classes?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
:focus
和:active
是两种不同的状态。:focus
表示当前选择元素接收输入时的状态,:active
表示当前元素被用户激活时的状态。例如,假设我们有一个
需要注意的是,当您单击某个元素时,您会为其提供焦点,这也会产生
:focus
和:active
相同的错觉。 它们不一样。单击时按钮处于:focus:active
状态。一个例子:
编辑: jsfiddle
:focus
and:active
are two different states.:focus
represents the state when the element is currently selected to receive input and:active
represents the state when the element is currently being activated by the user.For example let's say we have a
<button>
. The<button>
will not have any state to begin with. It just exists. If we use Tab to give "focus" to the<button>
, it now enters its:focus
state. If you then click (or press space), you then make the button enter its (:active
) state.On that note, when you click on an element, you give it focus, which also cultivates the illusion that
:focus
and:active
are the same. They are not the same. When clicked the button is in:focus:active
state.An example:
edit: jsfiddle
来源:CSS 伪类
Source: CSS Pseudo-classes
有四种情况。
:focus
(不处于活动状态)。:active
(没有焦点)。:active:focus
(同时活动和聚焦)。示例:
当页面加载时,两者都属于情况 1。当您按 Tab 键时,您将聚焦第二个 div 并看到它展示情况 2。当您单击第一个 div 时,您会看到情况 3。当您单击第二个 div,您会看到情况 4。
元素是否可聚焦是另一个问题。大多数都不是默认的。但是,可以安全地假设
、
、
There are four cases.
:focus
(without active).:active
(without focus).:active:focus
(active and focus simultaneously).Example:
When the page loads both are in case 1. When you press tab you will focus the second div and see it exhibit case 2. When you click on the first div you see case 3. When you click the second div, you see case 4.
Whether an element is focusable or not is another question. Most are not by default. But, it's safe to assume
<a>
,<input>
,<textarea>
are focusable by default.:focus
是指元素能够接受输入 - 输入框中的光标或已使用 Tab 键访问的链接。:active
是指用户激活元素的时间 - 用户按下鼠标按钮然后释放鼠标按钮之间的时间。:focus
is when an element is able to accept input - the cursor in a input box or a link that has been tabbed to.:active
is when an element is being activated by a user - the time between when a user presses a mouse button and then releases it.活动是当用户激活该点时(例如鼠标单击,如果我们在字段到字段之间使用选项卡,则没有活动样式的迹象。也许单击需要更多时间,只需尝试按住单击该点),焦点发生在之后该点被激活。试试这个:
Active is when the user activating that point (Like mouse clicking, if we use tab from field-to-field there is no sign from active style. Maybe clicking need more time, just try hold click on that point), focus is happened after the point is activated. Try this :
焦点只能通过键盘输入给出,但元素可以通过鼠标或键盘激活。
如果在链接上使用 :focus,则样式规则仅适用于按下键盘上的按钮。
Focus can only be given by keyboard input, but an Element can be activated by both, a mouse or a keyboard.
If one would use :focus on a link, the style rule would only apply with pressing a botton on the keyboard.
使用“焦点”将为键盘用户提供与鼠标用户悬停鼠标时相同的效果。在 Internet Explorer 中需要“Active”才能获得相同的效果。
现实情况是,这些状态并不适用于所有用户。不使用所有三个选择器会给许多无法使用鼠标的仅使用键盘的用户带来可访问性问题。我邀请您参加 #nomouse 挑战 (nomouse dot org)。
Using "focus" will give keyboard users the same effect that mouse users get when they hover with a mouse. "Active" is needed to get the same effect in Internet Explorer.
The reality is, these states do not work as they should for all users. Not using all three selectors creates accessibility issues for many keyboard-only users who are physically unable to use a mouse. I invite you to take the #nomouse challenge (nomouse dot org).