In JavaScript's DOM event model, events are registered by calling the addEventListener() method of an object. Usage is as follows: The parameters are described as follows:
Example 1 The following example uses addEventListener() to register click events for all buttons. First, call the getElementsByTagName() method of document to capture all button objects; then, use the for statement to traverse the button set (btn), and use the addEventListener() method to register an event function for each button to obtain the text displayed by the current object. <button id="btn1" onclick="btn1();">Button 1</button> <button id="btn2" onclick="btn2(event);">Button 2</button> <script> var btn = document.getElementsByTagName("button"); //Capture all buttons for(var i in btn){ //Traverse the button collection btn[i].addEventListener("click", function(){ alert(this.innerHTML); }, true); //Register an event handling function for each button object and define the response in the capture phase} </script> Preview in the browser and click different buttons, the browser will automatically display the button name. The effect is shown in the figure: Use the addEventListener() method to register the same event handling function for multiple objects, or to register multiple event handling functions for the same object. Registering multiple event handlers for the same object is very useful for modular development. Example 2 In the following example, two events are registered for paragraph text: mouseover and mouseout. When the cursor moves over paragraph text, it will be displayed with a blue background, and when the cursor moves out of the paragraph text, it will automatically be displayed with a red background. This way, there is no need to destroy the document structure and add multiple event attributes to the paragraph text. <p id="p1">Register multiple events for an object</p> <script> var p1 = document.getElementById("p1"); //Capture the handle of the paragraph element p1.addEventListener("mouseover", function () { this.style.background = 'blue'; }, true); //Register the first event handler for the paragraph element p1.addEventListener("mouseout", function () { this.style.background = 'blue'; }, true); //Register the second event handler for the paragraph element</script> The IE event model uses the attachEvent() method to register events. Usage is as follows: element.attachEvent(etype, eventName) The parameter list is as follows:
Example 3 In the following example, two events are registered for the paragraph tag <p>: mouseover and mouseout. When the cursor passes by, the background color of the paragraph text is blue, and when the cursor moves away, the background color is red. <p id="p1">Register multiple events for an object</p> <script> var p1 = document.getElementById("p1"); //Capture paragraph element p1.attachEvent("onmouseover", function () { this.style.background = 'blue'; }); //Register mouseover event p1.attachEvent("onmouseout", function () { this.style.background = 'red'; }); //Register mouseout event</script> When attachEvent() is used to register an event, the calling object of the event processing function is no longer the current event object itself, but the window object. Therefore, this in the event function points to the window, not the current object. If you want to get the current object, you should use the srcElement attribute of event. The first parameter of the attachEvent() method in the IE event model is the event type name, which needs to be prefixed with on. When using the addEventListener() method, the on prefix is not required, such as click. This is the end of this article about JS addEventListener() and attachEvent() methods to implement registration events. For more related JS addEventListener() and attachEvent() content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: JS removeAttribute() method to delete an attribute of an element
>>: Introduction and usage examples of ref and $refs in Vue
Table of contents 1. Installation and introductio...
When I configured mysql, I set the default storag...
Linux uses files as the basis to manage the devic...
In the front-end and back-end separation developm...
1. MySql Architecture Before introducing the stor...
Preface: js is a single-threaded language, so it ...
Due to your company standards, you may only allow...
Source of the problem: If the slave server is the...
Table of contents Introduction Four characteristi...
From development to deployment, do it yourself Wh...
Use CSS filter to write mouse over effect <div...
one. Why build a Nexus private server? All develo...
Create Table create table table name create table...
To understand what this means, we must first know ...
· 【Scene description】 After HTTP1.1, the HTTP pro...