HTML5 自动对焦 = 自动选择?
我正在使用一些 HTML5 表单功能来逐步增强我的 Web 项目的用户体验。其中一个特别是自动对焦,我想在允许用户输入标签的模态(灯箱)对话框中使用它。现有的标签可能在那里。
我安装的唯一支持自动对焦的浏览器是 Chrome 9,但是当我在那里测试它时,它确实将焦点放在该字段上,但也会自动选择该字段中的所有现有文本。我没想到或想要自动选择,我想要自动对焦。
是我的期望错误还是 Chrome 错误?
I'm using some HTML5 form features to progressively enhance the user experience of my web project. One in particular is autofocus, I'm wanting to use this in a modal (lightbox) dialog that allows users to enter tags. Existing tags could be there.
The only browser I have installed that supports autofocus is Chrome 9, however when I test it there it does place the focus on the field, but also automatically selects all existing text in the field. I did not expect or want autoselect, I want autofocus.
Is my expectation wrong or is Chrome wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Chrome 认为他们错了,并在 Chrome 36 中改变了这种行为。
我知道这一点是因为我错误地依赖 javascript .focus() 和 html autofocus 来选择文本。现在它需要一个 .select() 来选择并聚焦在文本字段中。很好,我们现在可以具体说明我们想要什么了:-)
现在 Chrome 的行为就像 Firefox 一样。
Chrome decided they were wrong and changed this behaviour in Chrome 36.
I know this because I was wrongly relying on javascript .focus() and html autofocus for also selecting the text. Now it requires a .select() to select and focus in the text field. Good that we can now be specific of what we want :-)
So now Chrome behaves like e.g. Firefox.
在这里阅读有关自动对焦和 html5 的更多信息: http://php.quicoto.com/autofocus-in- html5/
如果您有我们可以查看的代码片段,我们也许能够提供进一步的帮助。它看起来相当简单且实现起来毫不费力,并且表示除了 Fx 和 IE 之外的所有版本都支持该功能。
Read more about autofocus and html5 here: http://php.quicoto.com/autofocus-in-html5/
If you have a snippet of code we could look at, we might be able to further be of help. It looks fairly simple and painless to implement and says the feature is supported in everything but Fx and IE.
来自 HTML5规范,聚焦步骤没有规定是否应该选择文本。用户代理既不需要也不禁止选择文本。具体来说,以下是相关的聚焦步骤:
From the HTML5 specification, the focusing steps do not prescribe whether or not text should be selected. User agents are neither required nor forbidden to select the text. Specifically, here is the relevant focusing step:
它应该选择文本,因为
input.focus()
也这样做。It is supposed to select the text, as
input.focus()
does this too.