HTML DOM hasFocus()Method

元素对象参考手册Element Object

Examples

Output text information after the document gains focus:

var x = document.getElementById("demo");
if (document.hasFocus()) {
    x.innerHTML = "The document has gained focus.";
} else {
    x.innerHTML = "The document has lost focus.";
}

Try it Yourself »

Definition and Usage

The hasFocus() method returns a Boolean value, used to detect whether the document (or any element within the document) has focus.


Browser Support

The numbers in the table specify the first browser version that supports the method.

Method
hasFocus() 30.0 6.0 3.0 Yes 23.0

Syntax

document.hasFocus()

Parameters

None

Technical Details

Return Value: A Boolean value, used to detect whether the document or element has focus:

  • true - The document has focus
  • false - The document does not have focus

元素对象参考手册Element Object

Other Extensions