MDN wants to learn about developers like you: https://qsurvey.mozilla.com/s3/MDN-dev-survey

这篇翻译不完整。请帮忙从英语翻译这篇文章

createEvent使用的许多方法, 如 initCustomEvent, 都被废弃了. 请使用 event constructors 来替代.

 

创建一个指定类型的事件。其返回的对象必须先初始化并可以被传递给 element.dispatchEvent

语法

var event = document.createEvent(type);
  • event 就是被创建的 Event 对象.
  • type 是一个字符串,表示要创建的事件类型。事件类型可能包括"UIEvents", "MouseEvents", "MutationEvents", 或者 "HTMLEvents"。请查看 Notes 章节获取详细信息 。

示例

// Create the event.
var event = document.createEvent('Event');

// Define that the event name is 'build'.
event.initEvent('build', true, true);

// Listen for the event.
elem.addEventListener('build', function (e) {
  // e.target matches elem
}, false);

// target can be any Element or other EventTarget.
elem.dispatchEvent(event);

参考

注意

Event type字符串只能传递事件模块中定义的值给CreateEvent。其中一些事件模块是在DOM事件规范定义的,还有些事在其他规范定义的(如SVG),还有一些是Gecko-specific事件。详情见下表。

To-do: 添加事件名称到下表中。

事件模块 传递给 createEvent的Event type 事件初始化方法
DOM Level 2 Events
User Interface event module "UIEvents" event.initUIEvent
Mouse event module "MouseEvents" event.initMouseEvent
Mutation event module "MutationEvents" event.initMutationEvent
HTML event module "HTMLEvents" event.initEvent
DOM Level 3 Events
User Interface event module "UIEvent", "UIEvents" event.initUIEvent
Mouse event module "MouseEvent", "MouseEvents" event.initMouseEvent
Mutation event module "MutationEvent", "MutationEvents" event.initMutationEvent
Mutation name event module (not implemented in Gecko as of June 2006) "MutationNameEvent" event.initMutationNameEvent
Text event module "TextEvent" (Gecko also supports "TextEvents") event.initTextEvent (not implemented)
Keyboard event module "KeyboardEvent" (Gecko also supports "KeyEvents") event.initKeyEvent (Gecko-specific; the DOM 3 Events working draft uses initKeyboardEvent instead)
Custom event module "CustomEvent" event.initCustomEvent
Basic events module "Event" (Gecko also supports "Events") event.initEvent
SVG 1.1 Scripting
SVG "SVGEvents" (Gecko also supports "SVGEvent") event.initEvent
"SVGZoomEvents" (Gecko also supports "SVGZoomEvent") event.initUIEvent
Other event types supported by Gecko
- "MessageEvent" event.initMessageEvent
"MouseScrollEvents", "PopupEvents" event.initMouseEvent
"PopupBlockedEvents" event.initPopupBlockedEvent
"XULCommandEvent", "XULCommandEvents" event.initCommandEvent
Progress Events "ProgressEvent" ProgressEvent.initProgressEvent()已废弃 Gecko 22.0
Animation Events "AnimationEvent" (or "WebKitAnimationEvent" for WebKit/Blink-based browsers) AnimationEvent.initAnimationEvent()已废弃 Gecko 23.0
Transition Events "TransitionEvent" (or "WebKitTransitionEvent" for WebKit/Blink-based browsers) TransitionEvent.initTransitionEvent()已废弃 Gecko 23.0

有些事件可以使用两种事件类型参数,这是因为DOM Level 3 Events将事件类型参数更改为单数形式,但是仍然支持老的复数形式以向后兼容。

规范

 

<embed height="0" id="xunlei_com_thunder_helper_plugin_d462f475-c18e-46be-bd10-327458d045bd" type="application/thunder_download_plugin" width="0">

文档标签和贡献者

 此页面的贡献者: ucev, AspenLuoQiang
 最后编辑者: ucev,