使用 HTML5 自动对焦会将焦点设置为不可见的表单控件吗?
正如标题所述:即使使用 display: none;
或 visibility: hide 隐藏表单控件,焦点仍将设置在
autofocus="autofocus"
上;?
As the title states: Will focus still be set on form controls with autofocus="autofocus"
even if they are hidden with display: none;
or visibility: hidden;
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的问题是隐藏字段是否可以窃取可见字段的自动对焦,那么答案是否定的。
具有
autofocus
属性的隐藏字段在可见时获得焦点。这是一个 jsFiddle ,它显示了如果您有一个可见字段和一个隐藏字段会发生什么,然后显示隐藏字段。
这里有一个变体,演示了如果可见字段没有自动对焦会发生什么情况 属性。
If your question is whether a hidden field can steal autofocus from a visible one, the answer is no.
Hidden fields with an
autofocus
property get focus when they are made visible.Here's a jsFiddle that shows what happens if you have a visible field and a hidden field, then show the hidden field.
And here's a variation that demonstrates what happens if the visible field does not have an
autofocus
property.HTML5 草案标准仅要求一个元素是“可聚焦的”,其中可聚焦的意思是:
它确实说“但仅当元素正在渲染时...”并且标准将“渲染”定义为:
简而言之,答案似乎是,如果满足所有其他要求,则
display:none
将不会获得焦点,但display:hidden
将会获得焦点 - 假设所有浏览器实际上都遵循规格The HTML5 draft standard only requires that an element be "focusable" where focusable means:
It does say "but only if the element is either being rendered..." and the standard defines "rendered" as:
In short, the answer appears to be that if all other requirements are met then
display:none
won't be focused butdisplay:hidden
will - Assuming all browsers actually follow the spec.