Document: createEvent() method
{{APIRef("DOM")}}
[!WARNING] Many methods used with
createEvent, such asinitCustomEvent, 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 {{domxref("EventTarget.dispatchEvent")}}.
Syntax
createEvent(type)
Parameters
type- : 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.
- : A string that represents the type of event to be created. Possible event types include
Return value
An Event object.
Examples
// Create the event.
const event = document.createEvent("Event");
// Define that the event name is 'build'.
event.initEvent("build", true, true);
// Listen for the event.
elem.addEventListener("build", (e) => {
// e.target matches elem
});
// 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.
Specifications
{{Specifications}}
Browser compatibility
{{Compat}}
See also
这篇文档对你有帮助吗?
谢谢你的反馈!