且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

javascript / DOM事件名称约定

更新时间:2023-12-05 15:14:10

不幸的是,当处理旧约时,并不那么简单。 DOM事件在他们背后有很多历史。

Unfortunately, it's not so simple when you deal with legacy conventions. And DOM Events have a lot of history behind them.

以下是事件在DOM2事件规范中定义:

Here's how type attribute of Event is defined in DOM2 Events specification:


接口事件(在DOM级别2中引入)

类型(类型为DOMString,只读)

Interface Event (introduced in DOM Level 2)
type (of type DOMString, readonly)

事件的名称(不区分大小写)。该名称必须是XML名称。

The name of the event (case-insensitive). The name must be an XML name.

我认为这一点的推理在同一个文档中由本段解释: / p>

The reasoning behind this, I suppose, is explained by this paragraph in the same doc:


在HTML 4.0中,事件侦听器被指定为
元素的属性。[...]为了实现与HTML 4.0,
实现者可以查看代表事件
处理程序的属性的设置,作为
EventTarget上的EventListener的创建和注册。

In HTML 4.0, event listeners were specified as attributes of an element.[...] In order to achieve compatibility with HTML 4.0, implementors may view the setting of attributes which represent event handlers as the creation and registration of an EventListener on the EventTarget.

现在,DOM3中的立场发生了变化(事件名称为区分大小写),原来的方法是,我认为被认为是最安全的下注 - 所以不必依赖于用户代理的正确性(检查这个讨论这个有趣的问题作为例子)。

Now, while the stance has changed in DOM3 (where event names are case-sensitive), the original approach is, I suppose, ofter considered to be the safest bet - so one won't have to depend on user agents' correctness (check this discussion and this funny issue as examples).

请注意,W3C本身在DOM2中注册了一大堆CamelCase事件(所有的MutationEvents, DOMActivate DOMFocusIn DOMFocusOut )。

Note that W3C itself has registered a whole slew of CamelCased events in DOM2 (all the MutationEvents, DOMActivate, DOMFocusIn and DOMFocusOut).