


Detailed analysis of js native event description (code attached, simple and clear)
The following are the js native events I have compiled for you. Interested students can take a look.
1. Event flow
Event flow is mainly divided into two types: event bubbling and event capture. Events bubble up, and the target element receives the event first, and then gradually propagates upward to less specific nodes. Event capture is exactly the opposite, the main idea is that less specific nodes receive the event first and then gradually propagate down to the target node.
<html> <head> <title>事件流</title> </head> <body> <div id ="testDiv"></div> </body> </html>
When a div is clicked, the bubbling event receives the node order div->body->html->document
The capture event receives the node order document->html->body ->div
2. Event handler
a) HTML event handler
Each event supported by an element can be handled using an HTML attribute with the same name as the corresponding event handler. specified.
Disadvantages: 1. Time difference problem, the user may have clicked on the page element before the browser has parsed the click event function - wrap the error through try-catch
2. The scope chain of the event handler is different Browsers may produce different results.
3. HTML code and Javascript code are tightly coupled, which is not conducive to expansion and maintenance.
b) DOM0 level event handler
Assign the function to the element event handler attribute
var btn = document.getElementById(“#dv”);
btn.onclick = function(){} ;
The event handler of the element that you want to delete – btn.onclicn = null;
The event handler added in this way is processed in the event bubbling phase.
c)DOM2 level event handler
Specify event handler addEventListener
Remove event handler removeEventListener
Receive three parameters, namely the event name to be processed, the function of the event handler, Boolean Value (true means calling the handler in the capture phase, false means calling the event handler in the bubbling phase)
Note: addEventListener must be deleted through removeEventListener and the parameters must be consistent, so the anonymous function added through addEventListener cannot be removed.
d)IE事件处理程序
attachEvent
detachEvent
两个方法接收相同的两个参数(”onclick”,”function(){}”);
由于IE8级更早版本只支持事件冒泡,所以通过attachEvent添加的事件处理程序只能被添加到冒泡阶段。
注意:attach中第一个参数是onclick而不是addEventListener中的click
IE事件处理程序attachEvent添加的在全局作用域中运行
var btn = document.getElementById(“#tes”); btn.attachEvent(“onclick”,function(){ alert(this == window); //true })
3、跨浏览器的事件处理程序。
由于不同浏览器之间事件处理程序不同,所以有必要编写可以跨浏览器使用的事件处理程序。
var eventUtil = { addHandler:function(element,type,handler){ if(element.addEventListener){ element.addEventListener(type,handler,false); }else if(element.attachEvent){ element.attachEvent("on" + type,handler); }else{ element["on" + type] = handler; } }, removeHandler:function(element,type,handler){ if(element.removeEventListener){ element.removeEventListener(type,handler,false); }else if(element.deatchEvent){ element.deatch("on" + type,handler); }else{ element["on" + type] = null; } } }
上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
The above is the detailed content of Detailed analysis of js native event description (code attached, simple and clear). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

jQuery is a popular JavaScript library that can be used to simplify DOM manipulation, event handling, animation effects, etc. In web development, we often encounter situations where we need to change event binding on select elements. This article will introduce how to use jQuery to bind select element change events, and provide specific code examples. First, we need to create a dropdown menu with options using labels:

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service
