GlobalEventHandlers.onfocus - Web APIs 编辑

The onfocus property of the GlobalEventHandlers mixin is an EventHandler that processes focus events on the given element.

The focus event is raised when the user sets focus on an element.

For onfocus to fire on non-input elements, they must be given the tabindex attribute (see Building keyboard accessibility back in for more details).

Note: The opposite of onfocus is onblur.

Syntax

target.onfocus = functionRef;

Value

functionRef is a function name or a function expression. The function receives a FocusEvent object as its sole argument.

Example

This example uses onblur and onfocus to change the text within an <input> element.

HTML

<input type="text" value="CLICK HERE">

JavaScript

let input = document.querySelector('input');

input.onblur = inputBlur;
input.onfocus = inputFocus;

function inputBlur() {
  input.value = 'Focus has been lost';
}

function inputFocus() {
  input.value = 'Focus is here';
}

Result

Try clicking in and out of the form field, and watch its contents change accordingly.

Specifications

SpecificationStatusComment
HTML Living Standard
The definition of 'onfocus' in that specification.
Living Standard 

Browser compatibility

BCD tables only load in the browser

In contrast to IE, in which almost all kinds of elements receive the focus event, almost all kinds of elements on Gecko browsers do NOT work with this event.

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:106 次

字数:4165

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文