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.
<button id="btn">Click here!</button>
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!');
}
});
| 规范 | 状态 | 注释 |
|---|---|---|
|
DOM
The definition of 'EventListener' in that specification. |
实时标准 | 无变化。 |
|
DOM (文档对象模型) 2 级事件规范
The definition of 'EventListener' in that specification. |
过时 | 初始定义。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 |
完整支持