EventListener interface represents an object that can handle an event dispatched by an EventTarget 对象。

注意: Due to the need for compatibility with legacy content, EventListener accepts both a function and an object with a handleEvent() property function. This is shown in the 范例 下文。

特性

This interface neither implements, nor inherits, any properties.

方法

此接口未继承任何方法。

EventListener.handleEvent()

A function that is called whenever an event of the specified type occurs.

范例

HTML

<button id="btn">Click here!</button>
					

JavaScript

const buttonElement = document.getElementById('btn');
// Add a handler for the 'click' event by providing a callback function.
// Whenever the element is clicked, a pop-up with "Element clicked!" will
// appear.
buttonElement.addEventListener('click', function (event) {
  alert('Element clicked through function!');
});
// For compatibility, a non-function object with a `handleEvent` property is
// treated just the same as a function itself.
buttonElement.addEventListener('click', {
  handleEvent: function (event) {
    alert('Element clicked through handleEvent property!');
  }
});
					

结果

See Also:

规范

规范 状态 注释
DOM
The definition of 'EventListener' in that specification.
实时标准 无变化。
DOM (文档对象模型) 2 级事件规范
The definition of 'EventListener' in that specification.
过时 初始定义。

浏览器兼容性

The compatibility table in 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
EventListener 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
handleEvent 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

图例

完整支持

完整支持

元数据

  • 最后修改: