弃用
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 兼容性表格 at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

Event.initEvent() method is used to initialize the value of an event created using Document.createEvent() .

Events initialized in this way must have been created with the Document.createEvent() method. This method must be called to set the event before it is dispatched, using EventTarget.dispatchEvent() . Once dispatched, it doesn't do anything anymore.

Do not use this method anymore as it is deprecated.

Instead use specific event constructors, like Event() . The page on 创建和触发事件 gives more information about the way to use these.

句法

event.initEvent(type, bubbles, cancelable);
					
type
DOMString defining the type of event.
bubbles
布尔 deciding whether the event should bubble up through the event chain or not. Once set, the read-only property Event.bubbles will give its value.
cancelable
布尔 defining whether the event can be canceled. Once set, the read-only property Event.cancelable will give its value.

范例

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

// Create a click event that bubbles up and
// cannot be canceled 
event.initEvent('click', true, false);
// Listen for the event.
elem.addEventListener('click', function (e) {
  // e.target matches elem
}, false);
elem.dispatchEvent(event);
					

规范

规范 状态 注释
DOM
The definition of 'Event.initEvent()' in that specification.
实时标准 DOM (文档对象模型) 2 级事件规范 , deprecated it, superseded by event constructors.
DOM (文档对象模型) 2 级事件规范
The definition of 'Event.initEvent()' in that specification.
过时 初始定义。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
initEvent 弃用 Chrome Yes Edge 12 Firefox 17
17
不支持 ? — 17
Before Firefox 17, a call to this method after the dispatching of the event raised an exception instead of doing nothing.
IE Yes Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 17
17
不支持 ? — 17
Before Firefox 17, a call to this method after the dispatching of the event raised an exception instead of doing nothing.
Opera Android Yes Safari iOS Yes Samsung Internet Android Yes

图例

完整支持

完整支持

弃用。不要用于新网站。

弃用。不要用于新网站。

见实现注意事项。

另请参阅

  • The constructor to use instead of this deprecated method: Event() . More specific constructors can be used too.

元数据

  • 最后修改: