Dispatches an 事件 at the specified EventTarget , (synchronously) invoking the affected EventListener s in the appropriate order. The normal event processing rules (including the capturing and optional bubbling phase) also apply to events dispatched manually with dispatchEvent() .

句法

cancelled = !target.dispatchEvent(event)
					

参数

  • event 事件 object to be dispatched.
  • target is used to initialize the Event.target and determine which event listeners to invoke.

返回值

  • 返回值为 false if event is cancelable and at least one of the event handlers which received event called Event.preventDefault() . Otherwise it returns true .

dispatchEvent() method throws UNSPECIFIED_EVENT_TYPE_ERR if the event's type was not specified by initializing the event before the method was called, or if the event's type is null or an empty string.

异常

Exceptions thrown by event handlers are reported as uncaught exceptions. The event handlers run on a nested callstack; they block the caller until they complete, but exceptions do not propagate to the caller.

注意事项

Unlike "native" events, which are fired by the DOM and invoke event handlers asynchronously via the 事件循环 , dispatchEvent() invokes event handlers synchronously. All applicable event handlers will execute and return before the code continues on after the call to dispatchEvent() .

dispatchEvent() is the last step of the create-init-dispatch process, which is used for dispatching events into the implementation's event model. The event can be created using Event constructor .

另请参阅 Event object reference .

范例

创建和触发事件 .

规范

规范 状态 注释
DOM
The definition of 'EventTarget.dispatchEvent()' in that specification.
实时标准 Initial definition in the DOM 2 Events 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
dispatchEvent Chrome 4 Edge 12 Firefox 2 IE 9
9
不支持 6 — 11 Alternate Name
Older versions of IE supported an equivalent, proprietary EventTarget.fireEvent() 方法。
Alternate Name Uses the non-standard name: fireEvent
Opera 9 Safari 3.2 WebView Android 4 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 3 Samsung Internet Android 1.0

图例

完整支持

完整支持

见实现注意事项。

使用非标名称。

元数据

  • 最后修改: