HTML <button> autofocus 属性
一个布尔值的元素属性。用于指定当页面加载时,该按钮应当自动获得输入焦点。除非是用户重写,例如通过其他控件键入,才会失去焦点。一个文档中,只能有一个表单相关的元素可以指定该属性。
实例
按钮使用 autofocus 属性:
<button type="button" autofocus>页面加载后,我会获得焦点!</button>
语法
<button autofocus>
属性值
autofocus 属性是一个布尔值元素属性。
可选值的写法有:
-
<button autofocus></button>:表示启用 -
<button autofocus="autofocus"></button>:同上,表示启用 -
<button autofocus=""></button>:同上,表示启用 -
<button></button>:表示不启用
HTML 4.01 与 HTML5之间的差异
autofocus 属性是 HTML5 <button> 标签的新属性。
HTML 与 XHTML之间的差异
在 XHTML 中,不允许属性简写,autofocus 属性必须定义为 <button autofocus="autofocus">。
桌面浏览器兼容性
| Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| 5.0 | 12 | 4.0 (2.0) | 10 | 9.6 | 5.0 |
移动浏览器兼容性
| Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|
| 3 | 12 | 1.0 (1.0) | 10 | 37 | 未实现 |
HTML