EventListener - Web APIs 编辑
The EventListener
interface represents an object that can handle an event dispatched by an EventTarget
object.
Note: 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 example below.
Properties
This interface neither implements, nor inherits, any properties.
Methods
This interface doesn't inherit any methods.
EventListener.handleEvent()
- A function that is called whenever an event of the specified type occurs.
Example
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!');
}
});
Result
Specifications
Specification | Status | Comment |
---|---|---|
DOM The definition of 'EventListener' in that specification. | Living Standard | No change. |
Document Object Model (DOM) Level 2 Events Specification The definition of 'EventListener' in that specification. | Obsolete | Initial definition. |
Browser compatibility
BCD tables only load in the browser
See also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论