Document.createEvent() - Web APIs 编辑
Many methods used with createEvent
, such as initCustomEvent
, are deprecated. Use event constructors instead.
Creates an event of the type specified. The returned object should be first initialized and can then be passed to EventTarget.dispatchEvent
.
Syntax
var event = document.createEvent(type);
event
is the created Event object.type
is a string that represents the type of event to be created. Possible event types include"UIEvents"
,"MouseEvents"
,"MutationEvents"
, and"HTMLEvents"
. See Notes section for details.
Example
// 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);
Notes
Event type strings suitable for passing to createEvent()
are listed in the DOM standard — see the table in step 2. Bear in mind that most event objects now have constructors, which are the modern recommended way to create event object instances.
Gecko supports some non-standard event object aliases, which are listed below.
Event Module | Standard event object | Gecko also supports |
---|---|---|
Text event module | TextEvent | TextEvents |
Keyboard event module | KeyboardEvent | KeyEvents |
Basic events module | Event | Events |
Specifications
Specification | Status | Comment |
---|---|---|
DOM The definition of 'document.createEvent' in that specification. | Living Standard |
Browser compatibility
BCD tables only load in the browser
See also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论