Document.createEvent() - Web API 接口参考 编辑
createEvent使用的许多方法
, 如 initCustomEvent
, 都被废弃了. 请使用 event constructors 来替代.
创建一个指定类型的事件。其返回的对象必须先初始化并可以被传递给 element.dispatchEvent。
语法
var event = document.createEvent(type);
event
就是被创建的 Event 对象.type
是一个字符串,表示要创建的事件类型。事件类型可能包括"UIEvents"
,"MouseEvents"
,"MutationEvents"
, 或者"HTMLEvents"
。请查看 Notes 章节获取详细信息 。
示例
// 创建事件
var event = document.createEvent('Event');
// 定义事件名为'build'.
event.initEvent('build', true, true);
// 监听事件
elem.addEventListener('build', function (e) {
// e.target matches elem
}, false);
// 触发对象可以是任何元素或其他事件目标
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将事件类型参数更改为单数形式,但是仍然支持老的复数形式以向后兼容。
规范
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论