事件 接口表示在 DOM 中发生的事件。

事件可以由用户动作触发,如:点击鼠标按钮或敲击键盘,或由 API 生成来表示异步任务的进度。也可以通过编程方式触发,譬如通过调用 HTMLElement.click() 方法在元素,或通过定义事件,然后将其发送给指定目标使用 EventTarget.dispatchEvent() .

事件类型很多,其中一些使用其它接口基于主 事件 接口。 事件 本身包含所有事件共有的特性和方法。

可以把许多 DOM 元素设置为接受 (或监听) 这些事件,并执行代码以响应处理。事件处理程序通常被连接 (或附加) 到各种 HTML 元素 (譬如 <button> , <div> , <span> ,等) 使用 EventTarget.addEventListener() ,且一般来说这替换使用旧的 HTML 事件处理程序属性 。进一步,当适当添加时,还可以断开这种处理程序的连接若需要使用 removeEventListener() .

注意: 一个元素可以有几个这样的处理程序,即使对于精确相同的事件 — 特别是若将单独、独立代码模块附加给它们,每个模块有自己的独立目的。(例如:带有广告模块和统计模块的网页都在监视视频观看。)

当有许多嵌套元素且每个元素有自己的处理程序时,事件处理可能会变得很复杂 — 尤其是在父级元素收到与其子级元素非常相同事件的情况下,由于它们在空间上重叠,因此事件在技术上会同时发生,且这种事件的处理次序取决于 事件冒泡和捕获 设置当每个处理程序被触发时。

基于事件的接口

以下是接口的列表基于主 事件 接口,链接到它们在 MDN API 参考中各自的文档编制。

注意:所有事件接口名称均以 Event 结尾。

构造函数

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

特性

Event.bubbles 只读

布尔指示事件是否透过 DOM 冒泡。

Event.cancelBubble
历史别名为 Event.stopPropagation() 。把其值设为 true 在从事件处理程序返回之前阻止事件的传播。
Event.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
数组 of DOM 节点 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 只读
布尔 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()

取消事件 (若它可取消)。

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()

停止在 DOM 中进一步传播事件。

弃用方法

Event.createEvent()
创建新事件,然后必须被初始化通过调用其 initEvent() 方法。
Event.initEvent()

初始化创建事件的值。若事件已被分派,此方法什么都不做。

Event.getPreventDefault()
返回值为 Event.defaultPrevented .
Event.preventBubble()
阻止事件冒泡。使用 event.stopPropagation 代替。
Event.preventCapture()
阻止事件冒泡。使用 event.stopPropagation 代替。

规范

规范 状态 注释
DOM
在该规范中的 Event 定义。
实时标准

浏览器兼容性

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
事件 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 而不是 DOMTimeStamp .
Edge 12 Firefox Yes
Yes
Starting with Chrome 49, Firefox 54 and Opera 36, this property returns DOMHighResTimeStamp 而不是 DOMTimeStamp .
IE Yes
Yes
Starting with Chrome 49, Firefox 54 and Opera 36, this property returns DOMHighResTimeStamp 而不是 DOMTimeStamp .
Opera 36
36
Starting with Chrome 49, Firefox 54 and Opera 36, this property returns DOMHighResTimeStamp 而不是 DOMTimeStamp .
Safari Yes WebView Android 49
49
Starting with version 49, this property returns DOMHighResTimeStamp 而不是 DOMTimeStamp .
Chrome Android 49
49
Starting with Chrome 49, Firefox 54 and Opera 36, this property returns DOMHighResTimeStamp 而不是 DOMTimeStamp .
Firefox Android Yes
Yes
Starting with Chrome 49, Firefox 54 and Opera 36, this property returns DOMHighResTimeStamp 而不是 DOMTimeStamp .
Opera Android 36
36
Starting with Chrome 49, Firefox 54 and Opera 36, this property returns DOMHighResTimeStamp 而不是 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 而不是 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

图例

完整支持

完整支持

不支持

不支持

兼容性未知 ?

兼容性未知

非标。预期跨浏览器支持较差。

非标。预期跨浏览器支持较差。

弃用。不要用于新网站。

弃用。不要用于新网站。

见实现注意事项。

使用非标名称。

另请参阅

元数据

  • 最后修改:
  1. DOM (文档对象模型)
  2. 事件
  3. 构造函数
    1. 事件
  4. 特性
    1. bubbles
    2. cancelable
    3. cancelBubble
    4. composed
    5. currentTarget
    6. defaultPrevented
    7. eventPhase
    8. explicitOriginalTarget
    9. isTrusted
    10. originalTarget
    11. returnValue
    12. srcElement
    13. target
    14. timeStamp
    15. type
  5. 方法
    1. composedPath
    2. initEvent
    3. msConvertURL()
    4. preventDefault
    5. stopImmediatePropagation
    6. stopPropagation
  6. 事件
    1. bubbles
    2. cancelable
    3. cancelBubble
    4. composed
    5. composedPath
    6. currentTarget
    7. defaultPrevented
    8. 事件
    9. eventPhase
    10. initEvent
    11. isTrusted
    12. preventDefault
    13. returnValue
    14. stopImmediatePropagation
    15. stopPropagation
    16. target
    17. timeStamp
    18. type
  7. DOM 相关页面
    1. AbortController
    2. AbortSignal
    3. AbstractRange
    4. Attr
    5. ByteString
    6. CDATASection
    7. CSSPrimitiveValue
    8. CSSValue
    9. CSSValueList
    10. CharacterData
    11. ChildNode
    12. 注释
    13. CustomEvent
    14. DOMConfiguration
    15. DOMError
    16. DOMErrorHandler
    17. DOMException
    18. DOMImplementation
    19. DOMImplementationList
    20. DOMImplementationRegistry
    21. DOMImplementationSource
    22. DOMLocator
    23. DOMObject
    24. DOMParser
    25. DOMPoint
    26. DOMPointInit
    27. DOMPointReadOnly
    28. DOMRect
    29. DOMString
    30. DOMTimeStamp
    31. DOMTokenList
    32. DOMUserData
    33. Document
    34. DocumentFragment
    35. DocumentType
    36. 元素
    37. ElementTraversal
    38. Entity
    39. EntityReference
    40. EventTarget
    41. HTMLCollection
    42. MutationObserver
    43. 节点
    44. NodeFilter
    45. NodeIterator
    46. NodeList
    47. NonDocumentTypeChildNode
    48. ProcessingInstruction
    49. PromiseResolver
    50. 范围
    51. StaticRange
    52. 文本
    53. TextDecoder
    54. TextEncoder
    55. TimeRanges
    56. TreeWalker
    57. TypeInfo
    58. USVString
    59. UserDataHandler
    60. XMLDocument

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1