Event

Event interface represents an event which takes place in the DOM.

An event can be triggered by the user action e.g. clicking the mouse button or tapping keyboard, or generated by APIs to represent the progress of an asynchronous task. It can also be triggered programmatically, such as by calling the HTMLElement.click() method of an element, or by defining the event, then sending it to a specified target using EventTarget.dispatchEvent() .

There are many types of events, some of which use other interfaces based on the main Event 接口。 Event itself contains the properties and methods which are common to all events.

Many DOM elements can be set up to accept (or "listen" for) these events, and execute code in response to process (or "handle") them. Event-handlers are usually connected (or "attached") to various HTML elements (譬如 <button> , <div> , <span> , etc.) using EventTarget.addEventListener() , and this generally replaces using the old HTML event handler attributes . Further, when properly added, such handlers can also be disconnected if needed using removeEventListener() .

注意: One element can have several such handlers, even for the exact same event—particularly if separate, independent code modules attach them, each for its own independent purposes. (For example, a webpage with an advertising-module and statistics-module both monitoring video-watching.)

When there are many nested elements, each with its own handler(s), event processing can become very complicated—especially where a parent element receives the very same event as its child elements because "spatially" they overlap so the event technically occurs in both, and the processing order of such events depends on the Event bubbling and capture settings of each handler triggered.

基于事件的接口

Below is a list of interfaces which are based on the main Event interface, with links to their respective documentation in the MDN API reference.

Note that all event interfaces have names which end in "Event".

构造函数

Event()
创建 Event 对象,将其返回给调用者。

特性

Event.bubbles 只读
A boolean indicating whether or not the event bubbles up through the DOM.
Event.cancelBubble
A historical alias to Event.stopPropagation() . Setting its value to true before returning from an event handler prevents propagation of the event.
Event.cancelable 只读
A boolean indicating whether the event is cancelable.
Event.composed 只读
A boolean indicating whether or not the event can bubble across the boundary between the shadow DOM and the regular DOM.
Event.currentTarget 只读
A reference to the currently registered target for the event. This is the object to which the event is currently slated to be sent. It's possible this has been changed along the way through retargeting .
Event.deepPath
An Array of DOM Node s through which the event has bubbled.
Event.defaultPrevented 只读
Indicates whether or not the call to event.preventDefault() canceled the event.
Event.eventPhase 只读
Indicates which phase of the event flow is being processed.
Event.explicitOriginalTarget 只读
The explicit original target of the event (Mozilla-specific.)
Event.originalTarget 只读
The original target of the event, before any retargetings. (Mozilla-specific.)
Event.returnValue
A historical property introduced by Internet Explorer and eventually adopted into the DOM specification in order to ensure existing sites continue to work. Ideally, you should try to use Event.preventDefault() and Event.defaultPrevented instead, but you can use returnValue if you choose to do so.
Event.srcElement
A non-standard alias (from old versions of Microsoft Internet Explorer) for Event.target . Some other browsers are starting to support it for web compatibility purposes.
Event.target 只读
A reference to the target to which the event was originally dispatched.
Event.timeStamp 只读
The time at which the event was created (in milliseconds). By specification, this value is time since epoch—but in reality, browsers' definitions vary. In addition, work is underway to change this to be a DOMHighResTimeStamp 代替。
Event.type 只读
The name of the event. Case-insensitive.
Event.isTrusted 只读
Indicates whether or not the event was initiated by the browser (after a user click, for instance) or by a script (using an event creation method, like Event.initEvent ).

弃用特性

Event.scoped 只读
A Boolean indicating whether the given event will bubble across through the shadow root into the standard DOM. Use composed 代替。

方法

Event.composedPath()
Returns the event’s path (objects on which listeners will be invoked). This does not include nodes in shadow trees if the shadow root was created with its ShadowRoot.mode closed.
Event.preventDefault()
Cancels the event (if it is cancelable).
Event.stopImmediatePropagation()
For this particular event, prevent all other listeners from being called. This includes listeners attached to the same element as well as those attached to elements that will be traversed later (during the capture phase, for instance).
Event.stopPropagation()
Stops the propagation of events further along in the DOM.

弃用方法

Event.createEvent()
Creates a new event, which must then be initialized by calling its initEvent() 方法。
Event.initEvent()
Initializes the value of an Event created. If the event has already been dispatched, this method does nothing.
Event.getPreventDefault()
Returns the value of Event.defaultPrevented .
Event.preventBubble()
Prevents the event from bubbling. Use event.stopPropagation 代替。
Event.preventCapture()
Prevents the event from bubbling. Use event.stopPropagation 代替。

规范

规范 状态 Comment
DOM
The definition of 'Event' in that specification.
实时标准

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
Event Chrome 1 Edge 12 Firefox 1 IE 6 Opera 4 Safari 1 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0
Event() 构造函数 Chrome 15 Edge 12 Firefox 11 IE No Opera 11.6 Safari 6 WebView Android ≤37 Chrome Android 18 Firefox Android 14 Opera Android 12 Safari iOS 6 Samsung Internet Android 1.0
bubbles Chrome Yes Edge 12 Firefox Yes IE ? Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
cancelable Chrome Yes Edge 12 Firefox Yes IE ? Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
cancelBubble Chrome Yes
Yes
Starting with Chrome 58 and Opera 45, setting this property to false does nothing, as per spec discussion .
Edge 12 Firefox 53
53
Prior to Firefox 53, this property was defined on the UIEvent interface. See bug 1298970 了解更多细节。
IE Yes Opera Yes
Yes
Starting with Chrome 58 and Opera 45, setting this property to false does nothing, as per spec discussion .
Safari Yes WebView Android Yes
Yes
Starting with Chrome 58 and Opera 45, setting this property to false does nothing, as per spec discussion .
Chrome Android Yes
Yes
Starting with Chrome 58 and Opera 45, setting this property to false does nothing, as per spec discussion .
Firefox Android 53
53
Prior to Firefox 53, this property was defined on the UIEvent interface. See bug 1298970 了解更多细节。
Opera Android Yes
Yes
Starting with Chrome 58 and Opera 45, setting this property to false does nothing, as per spec discussion .
Safari iOS Yes Samsung Internet Android Yes
Yes
Starting with Samsung Internet 7.0 and Opera 45, setting this property to false does nothing, as per spec discussion .
composed Chrome 53 Edge 79 Firefox 52 IE No Opera 40 Safari Yes WebView Android 53 Chrome Android 53 Firefox Android 52 Opera Android 41 Safari iOS Yes Samsung Internet Android 6.0
composedPath Chrome 53
53
不支持 50 — 53
Alternate Name
Alternate Name Uses the non-standard name: deepPath
Edge 79 Firefox 52 IE No Opera 40
40
不支持 37 — 40
Alternate Name
Alternate Name Uses the non-standard name: deepPath
Safari 10 WebView Android 53
53
不支持 50 — 53
Alternate Name
Alternate Name Uses the non-standard name: deepPath
Chrome Android 53
53
不支持 50 — 53
Alternate Name
Alternate Name Uses the non-standard name: deepPath
Firefox Android 52 Opera Android 41
41
不支持 37 — 41
Alternate Name
Alternate Name Uses the non-standard name: deepPath
Safari iOS 10 Samsung Internet Android 6.0
6.0
不支持 5.0 — 6.0
Alternate Name
Alternate Name Uses the non-standard name: deepPath
currentTarget Chrome 1 Edge 12 Firefox 1 IE 9
9
不支持 6 — 9
On Internet Explorer 6 through 8, the event model is different. Event listeners are attached with the non-standard EventTarget.attachEvent method. In this model, there is no equivalent to event.currentTarget and this is the global object. One solution to emulate the event.currentTarget feature is to wrap your handler in a function calling the handler using Function.prototype.call with the element as a first argument. This way, this will be the expected value.
Opera 7 Safari 10 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 10 Samsung Internet Android 1.0
defaultPrevented Chrome 18 Edge 12 Firefox 6 IE 9 Opera 11 Safari 5 WebView Android Yes Chrome Android 18 Firefox Android 6 Opera Android 11 Safari iOS 5 Samsung Internet Android 1.0
eventPhase Chrome 45 Edge 12 Firefox Yes IE 9 Opera 32 Safari Yes WebView Android 45 Chrome Android 45 Firefox Android Yes Opera Android 32 Safari iOS Yes Samsung Internet Android 5.0
explicitOriginalTarget
非标
Chrome No Edge No Firefox Yes IE No Opera No Safari No WebView Android No Chrome Android No Firefox Android Yes Opera Android No Safari iOS No Samsung Internet Android No
getPreventDefault
弃用 非标
Chrome No Edge No Firefox ? — 59
不支持 ? — 59
bug 691151 .
IE ? Opera No Safari ? WebView Android No Chrome Android No Firefox Android ? — 59
不支持 ? — 59
bug 691151 .
Opera Android No Safari iOS ? Samsung Internet Android No
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
isTrusted Chrome 46
46
Starting with Chrome 53 and Opera 40, untrusted events do not invoke the default action.
Edge 12 Firefox Yes IE No
不支持 No
In Internet Explorer, all events are trusted except those that are created with the createEvent() 方法。
Opera 33
33
Starting with Chrome 53 and Opera 40, untrusted events do not invoke the default action.
Safari 10 WebView Android 46
46
Starting with version 53, untrusted events do not invoke the default action.
Chrome Android 46
46
Starting with Chrome 53 and Opera 40, untrusted events do not invoke the default action.
Firefox Android Yes Opera Android 33
33
Starting with Chrome 53 and Opera 40, untrusted events do not invoke the default action.
Safari iOS 10 Samsung Internet Android 5.0
5.0
Starting with Samsung Internet 6.0 and Opera 40, untrusted events do not invoke the default action.
originalTarget
非标
Chrome No Edge No Firefox Yes IE No Opera No Safari No WebView Android No Chrome Android No Firefox Android Yes Opera Android No Safari iOS No Samsung Internet Android No
preventBubble
弃用 非标
Chrome No Edge No Firefox ? — 24 IE ? Opera No Safari ? WebView Android No Chrome Android No Firefox Android ? — 24 Opera Android No Safari iOS ? Samsung Internet Android No
preventCapture
弃用 非标
Chrome No Edge No Firefox ? — 24 IE ? Opera No Safari ? WebView Android No Chrome Android No Firefox Android ? — 24 Opera Android No Safari iOS ? Samsung Internet Android No
preventDefault Chrome 1 Edge 12 Firefox 1 IE 9 Opera 7 Safari 1 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0
returnValue
弃用
Chrome Yes Edge 12 Firefox No
不支持 No
Temporarily added in 63, removed in 64, briefly added in 65, then removed again while related compatibility issues are sorted out (see bug 1520756 ).
IE 6 Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android No
不支持 No
Temporarily added in 63, removed in 64, briefly added in 65, then removed again while related compatibility issues are sorted out (see bug 1520756 ).
Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
srcElement
弃用
Chrome Yes Edge 12 Firefox 62 IE Yes Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 62 Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
stopImmediatePropagation Chrome 6 Edge 12 Firefox 10 IE 9 Opera 15 Safari 5 WebView Android ≤37 Chrome Android 18 Firefox Android 10 Opera Android 14 Safari iOS 5 Samsung Internet Android 1.0
stopPropagation Chrome 1 Edge 12 Firefox 1 IE 9 Opera 7 Safari 1 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0
target Chrome 1 Edge 12 Firefox 1 IE 9 Opera 7 Safari 1 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0
timeStamp Chrome 49
49
Starting with Chrome 49, Firefox 54 and Opera 36, this property returns DOMHighResTimeStamp instead of DOMTimeStamp .
Edge 12 Firefox Yes
Yes
Starting with Chrome 49, Firefox 54 and Opera 36, this property returns DOMHighResTimeStamp instead of DOMTimeStamp .
IE Yes
Yes
Starting with Chrome 49, Firefox 54 and Opera 36, this property returns DOMHighResTimeStamp instead of DOMTimeStamp .
Opera 36
36
Starting with Chrome 49, Firefox 54 and Opera 36, this property returns DOMHighResTimeStamp instead of DOMTimeStamp .
Safari Yes WebView Android 49
49
Starting with version 49, this property returns DOMHighResTimeStamp instead of DOMTimeStamp .
Chrome Android 49
49
Starting with Chrome 49, Firefox 54 and Opera 36, this property returns DOMHighResTimeStamp instead of DOMTimeStamp .
Firefox Android Yes
Yes
Starting with Chrome 49, Firefox 54 and Opera 36, this property returns DOMHighResTimeStamp instead of DOMTimeStamp .
Opera Android 36
36
Starting with Chrome 49, Firefox 54 and Opera 36, this property returns DOMHighResTimeStamp instead of DOMTimeStamp .
Safari iOS Yes Samsung Internet Android 5.0
5.0
Starting with Samsung Internet 5.0, Firefox 54 and Opera 36, this property returns DOMHighResTimeStamp instead of DOMTimeStamp .
type Chrome 1 Edge 12 Firefox 1 IE 9 Opera 7 Safari 1 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0

图例

完整支持
完整支持
不支持
不支持
兼容性未知
兼容性未知
非标。预期跨浏览器支持较差。
非标。预期跨浏览器支持较差。
弃用。不要用于新网站。
弃用。不要用于新网站。
见实现注意事项。
使用非标名称。

另请参阅