HTMLElement.focus()
method sets focus on the specified element, if it can be focused. The focused element is the element which will receive keyboard and similar events by default.
element.focus(options); // Object parameter
选项
可选
An optional object providing options to control aspects of the focusing process. This object may contain the following property:
preventScroll
可选
false
for
preventScroll
(the default) means that the browser will scroll the element into view after focusing it. If
preventScroll
被设为
true
, no scrolling will occur.
focusMethod = function getFocus() {
document.getElementById("myTextField").focus();
}
<input type="text" id="myTextField" value="Text field."> <p></p> <button type="button" onclick="focusMethod()">Click me to focus on the text field!</button>
focusMethod = function getFocus() {
document.getElementById("myButton").focus();
}
<button type="button" id="myButton">Click Me!</button> <p></p> <button type="button" onclick="focusMethod()">Click me to focus on the button!</button>
focusScrollMethod = function getFocus() {
document.getElementById("myButton").focus({preventScroll:false});
}
focusNoScrollMethod = function getFocusWithoutScrolling() {
document.getElementById("myButton").focus({preventScroll:true});
}
<button type="button" onclick="focusScrollMethod()">Click me to focus on the button!</button> <button type="button" onclick="focusNoScrollMethod()">Click me to focus on the button without scrolling!</button> <div id="container" style="height: 1000px; width: 1000px;"> <button type="button" id="myButton" style="margin-top: 500px;">Click Me!</button> </div>
HTMLElement.focus()
from a mousedown event handler, you must call
event.preventDefault()
to keep the focus from leaving the
HTMLElement
Behaviour of the focus in relation to different HTML features like
tabindex
or
shadow dom
, which previously remained under-specified, were recently updated (as October of 2019). Checkout
WHATWG blog
for more info.
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
focus
|
Chrome 1 | Edge 12 | Firefox 5 | IE 9 | Opera 8 | Safari 3 | WebView Android 4.4 | Chrome Android 18 | Firefox Android 5 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 |
preventScroll
Boolean option
|
Chrome 64 | Edge ≤79 | Firefox 68 | IE ? | Opera 51 | Safari ? | WebView Android 64 | Chrome Android 64 | Firefox Android 68 | Opera Android 47 | Safari iOS ? | Samsung Internet Android 9.0 |
完整支持
兼容性未知
HTMLElement.blur()
to remove the focus from an element.
document.activeElement
to know which is the currently focused element.
HTMLOrForeignElement
blur()
focus()
BeforeUnloadEvent
DOMStringMap
ErrorEvent
GlobalEventHandlers
HTMLAnchorElement
HTMLAreaElement
HTMLAudioElement
HTMLBRElement
HTMLBaseElement
HTMLBaseFontElement
HTMLBodyElement
HTMLButtonElement
HTMLCanvasElement
HTMLContentElement
HTMLDListElement
HTMLDataElement
HTMLDataListElement
HTMLDialogElement
HTMLDivElement
HTMLDocument
HTMLElement
HTMLEmbedElement
HTMLFieldSetElement
HTMLFormControlsCollection
HTMLFormElement
HTMLFrameSetElement
HTMLHRElement
HTMLHeadElement
HTMLHeadingElement
HTMLHtmlElement
HTMLIFrameElement
HTMLImageElement
HTMLInputElement
HTMLIsIndexElement
HTMLKeygenElement
HTMLLIElement
HTMLLabelElement
HTMLLegendElement
HTMLLinkElement
HTMLMapElement
HTMLMediaElement
HTMLMetaElement
HTMLMeterElement
HTMLModElement
HTMLOListElement
HTMLObjectElement
HTMLOptGroupElement
HTMLOptionElement
HTMLOptionsCollection
HTMLOutputElement
HTMLParagraphElement
HTMLParamElement
HTMLPictureElement
HTMLPreElement
HTMLProgressElement
HTMLQuoteElement
HTMLScriptElement
HTMLSelectElement
HTMLShadowElement
HTMLSourceElement
HTMLSpanElement
HTMLStyleElement
HTMLTableCaptionElement
HTMLTableCellElement
HTMLTableColElement
HTMLTableDataCellElement
HTMLTableElement
HTMLTableHeaderCellElement
HTMLTableRowElement
HTMLTableSectionElement
HTMLTemplateElement
HTMLTextAreaElement
HTMLTimeElement
HTMLTitleElement
HTMLTrackElement
HTMLUListElement
HTMLUnknownElement
HTMLVideoElement
HashChangeEvent
历史
ImageData
定位
MessageChannel
MessageEvent
MessagePort
Navigator
NavigatorGeolocation
NavigatorID
NavigatorLanguage
NavigatorOnLine
NavigatorPlugins
PageTransitionEvent
Plugin
PluginArray
PopStateEvent
PortCollection
PromiseRejectionEvent
RadioNodeList
Transferable
ValidityState
Window
WindowBase64
WindowEventHandlers
WindowTimers