Button autofocus 属性

定义和用法

autofocus 属性设置或返回按钮是否在页面加载时自动获得焦点。

此属性反映 HTML autofocus 属性

注释:autofocus 属性 是 HTML5 中 <button> 元素的新属性。

实例

查看按钮是否在页面加载时自动获得焦点:

var x = document.getElementById("myBtn").autofocus;

亲自试一试

语法

返回 autofocus 属性:

buttonObject.autofocus

设置 autofocus 属性:

buttonObject.autofocus = true|false

属性值

描述
true|false

Specifies whether the button should receive focus when the page loads.

  • true - the button receives focus
  • false - default. The button does not receive focus

Technical details

Return value: Boolean value, returns true if the button automatically focuses when the page loads, otherwise returns false.

Browser support

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
Support 10.0 Support Support Support

Related pages

HTML reference manual:HTML <button> autofocus attribute