The 'addEventListener' method is used to attach an event listener to an HTML element. It takes two parameters: the event type and the function to be executed when the event is triggered.

Here is an example of how to use 'addEventListener':

const button = document.querySelector('button');

button.addEventListener('click', function() {
  console.log('Button clicked!');
});

In the example above, the 'addEventListener' method is used to attach a click event listener to a button element. When the button is clicked, the anonymous function will be executed and the message 'Button clicked!' will be logged to the console.

JavaScript addEventListener() Method: Attach Event Listeners to HTML Elements

原文地址: https://www.cveoy.top/t/topic/pmOQ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录