Document: phương thức createEvent()
Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.
Warning:
Nhiều phương thức được sử dụng với createEvent, chẳng hạn như initCustomEvent, đã lỗi thời.
Thay vào đó, hãy sử dụng event constructors.
Tạo một event thuộc loại được chỉ định. Đối tượng được trả về trước tiên cần được khởi tạo và sau đó có thể được truyền cho EventTarget.dispatchEvent.
Cú pháp
createEvent(type)
Tham số
Giá trị trả về
Một đối tượng Event.
Ví dụ
// 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);
Ghi chú
Các chuỗi loại sự kiện phù hợp để truyền cho createEvent() được liệt kê trong tiêu chuẩn DOM — xem bảng trong bước 2. Lưu ý rằng hầu hết các đối tượng sự kiện hiện có constructors, đây là cách được khuyến nghị hiện đại để tạo các instance đối tượng sự kiện.
Đặc tả kỹ thuật
| Thông số kỹ thuật |
|---|
| DOM> # dom-document-createevent> |