当将焦点放在新元素上时,是否可以保证blur()事件在focus()之前发生? [英] Is it guaranteed that blur() event happens before the focus() when giving focus to a new element?

查看:95
本文介绍了当将焦点放在新元素上时,是否可以保证blur()事件在focus()之前发生?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个输入字段。

一个具有焦点,然后单击另一个输入字段。

Let's say I have two input fields.
One has focus, then I click on the other input field.

它是保证 之前 之前的 blur()事件(失去焦点的第一个元素) focus()事件(其他元素获得了焦点)?

Is it guaranteed that the blur() event (of the first element that lost focus) happens before the focus() event (other element got focus) ?

推荐答案

从W3C草案开始 DOM3事件您:

焦点事件顺序


发生本规范中定义的焦点事件相对于彼此以固定顺序
。以下是当焦点在元素之间转移时
事件的典型顺序(此顺序假定
最初没有焦点被聚焦):

The focus events defined in this specification occur in a set order relative to one another. The following is the typical sequence of events when a focus is shifted between elements (this order assumes that no element is initially focused):



> User shifts focus
> 1.    focusin Sent before first target element receives focus
> 2.    focus   Sent after first target element receives focus 
> User shifts focus
> 3.    focusout    Sent before first target element loses focus
> 4.    focusin Sent before second target element receives focus
> 5.    blur    Sent after first target element loses focus
> 6.    focus   Sent after second target element receives focus

请注意,声明不能有两个元素重点不完全正确;尽管我不知道让两个小部件同时聚焦的桌面环境,但是该规范使实现可以决定:

Note that the statement that there cannot be two elements with focus is not completely correct; although I am not aware of desktop environments that let two widgets have focus at the same time, the specification lets an implementation decide that:


Other规范可能定义了比本规范中描述的更为复杂的焦点模型,包括允许多个元素具有当前焦点。

Other specifications may define a more complex focus model than is described in this specification, including allowing multiple elements to have the current focus.

这也值得一提:


宿主语言可以定义可能收到的特定元素焦点,元素可能获得焦点的条件,焦点可能改变的方式以及焦点变化的顺序 。 (斜体字是我的)。

A host language may define specific elements which might receive focus, the conditions under which an element may receive focus, the means by which focus may be changed, and the order in which the focus changes. (italic is mine).

但是,请注意Chrome浏览器未遵循标准-而是使用模糊,聚焦,聚焦,聚焦。您可以使用此页面进行测试。 Webkit浏览器可能有相同的问题。自 focusin 和 focusout 事件-US / Firefox / Releases / 52 rel = noreferrer> Firefox 52

However, beware that Chrome does not follow the standard - instead the order is blur, focusout, focus, focusin. You can test this using this page. Webkit browsers may have the same issue. The focusin and focusout events are supported since Firefox 52.

这篇关于当将焦点放在新元素上时,是否可以保证blur()事件在focus()之前发生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆