HTMLSelectElement.autofocus - Web APIs 编辑
The HTMLSelectElement.autofocus
property is a Boolean
that reflects the autofocus
HTML attribute, which indicates whether the associated <select>
element will get input focus when the page loads, unless the user overrides it.
Only one form-associated element in a document can have this attribute specified. If there are several, the first element with the attribute set inserted, usually the first such element on the page, get the initial focus.
Setting this property doesn't set the focus to the associated <select>
element: it merely tells the browser to focus to it when the element is inserted in the document. Setting it after the insertion, that is most of the time after the document load, has no visible effect.
SyntaxEdit
aBool = aSelectElement.autofocus; // Get the value of autofocus aSelectElement.autofocus = aBool; // Set the value of autofocus
Example
HTML
<select id="mySelect" autofocus>
<option>Option 1</option>
<option>Option 2</option>
</select>
JavaScript
// Check if the autofocus attribute on the <select>
var hasAutofocus = document.getElementById('mySelect').autofocus;
Specifications
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'autofocus' in that specification. | Living Standard | |
HTML 5.2 The definition of 'autofocus' in that specification. | Recommendation |
Browser compatibility
BCD tables only load in the browser
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论