HTML DOM focus()Method

元素对象参考手册Element Object

Example

Set focus to an <a> element:

document.getElementById("myAnchor").focus();

Try it Yourself »

Definition and Usage

The focus() method is used to set focus to an element (if it can be set).

Tip:Useblur()the method to remove element focus.


Browser Support

Method
focus() Yes Yes Yes Yes Yes

Syntax

HTMLElementObject.focus()

Parameters

None

Technical Details

Return Value: No return value

Examples

More Examples

Example

Set focus to a text field:

document.getElementById("myText").focus();

Try it Yourself »

Example

Set focus to a text field after the document has loaded:

window.onload = function() {
    document.getElementById("myText").focus();
};

Try it Yourself »

Related Pages

HTML DOM Reference:onfocus Event


元素对象参考手册Element Object

Other Extensions