


[JavaScript] Two completely opposite event flows: event bubbling and event capturing
What is an event?
Events are specific moments of interaction that occur between a document and a browser window.
What is event flow:
Event flow describes the order in which events are received from the page, but interestingly, Microsoft (IE) and Netscape (Netscape) ) The development team actually proposed two completely opposite event stream concepts. IE's event stream is event bubbling stream (event bubbling), while Netscape's event stream is event capturing stream (event capturing).
The first type: event bubbling
The event flow proposed by IE is called event bubbling, that is, the event starts with the most specific element Receive, and then propagate up to less specific nodes step by step
p——>body——>html——>document
Second type: event capture
Less specific nodes should receive events earlier, and the most specific nodes should receive events last. The purpose of capture is to capture an event before it reaches its intended destination.
document——>html——>body——>p
DOM event flow
"DOM2 level event" specified The event flow includes three stages: event capture stage, target stage, event bubbling stage
In the DOM event flow, the actual target will not receive events during the capture phase, which means that during the capture phase, The event stops after it reaches the body. The next stage is in the target stage, so the event occurs on p and is considered part of the bubbling stage in event processing. Then, the bubbling phase occurs and the event is propagated back to the document.
Even though the "DOM2-level events" specification clearly requires that the capture phase does not involve the event target, current mainstream browsers will trigger events on the event object during the capture phase. The result is that there are two opportunities to manipulate events on the target object.
DOM level 2 event handler
DOM level 2 events define two methods: operations for processing add events and deletion events:
Add event addEventListener() Delete event removeEventListener()
All DOM nodes contain these two methods, and they both contain 3 parameters: (1) The event method to be processed (for example : click, mouseover, dbclick...) (2) The event processing function can be an anonymous function or a named function (but if you need to delete the event, it must be a named function) (3) A Boolean value, representing Is it in the event bubbling stage processing or event capturing stage (true: means calling the event handler in the capturing stage; false: means calling the event handler in the bubbling stage)
//这是一个DOM 2级事件 添加事件最简单的方式(此时添加的是一个匿名函数) <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <button>按钮</button> <script> var btn=document.querySelector('button'); btn.addEventListener('click',function(){ console.log('我是按钮') },false) //当第三个参数不写时,也是默认为false(冒泡时添加事件) </script> </body> </html>
//添加的函数是命名函数,removeEventListener需要用这种方式 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <button>按钮</button> <script> var btn=document.querySelector('button'); btn.addEventListener('click',foo,false); function foo(){ console.log('我是按钮') } //其实操作就是把写在里面的函数拿到了外面,而在原来的位置用函数名来代替 </script> </body> </html>
DOM2-level handler also supports adding two If an event handles an event, both events will be executed.
In most cases, we add the event handler to the bubbling phase of the event stream, so as to maximize compatibility. Various browsers.
It is best to only add event handlers to the capture phase when it is necessary to intercept the event before it reaches the target.
If it is not specifically needed, it is not recommended to register an event handler during the event capture phase. Prevent event bubblingIt is mainly used to prevent event propagation. Prevents it from being dispatched to other DOM nodes and can be used at any stage of event propagation. The usage method is as follows (compatible with IE):function stopBubble(event){ if(window.event){ //兼容IE window.event.cancelBubble=true; }else{ event.stopPropagation(); } }
function stopDefaultEvent(event){
if(window.event){
//兼容IE
window.event.returnValue=false;
}else{
event.preventDefault()
}
return false;
}
Copy after loginRelated articles:
function stopDefaultEvent(event){ if(window.event){ //兼容IE window.event.returnValue=false; }else{ event.preventDefault() } return false; }
Detailed explanation of JS bubbling events and event capture examples
How to implement JavaSript event bubbling and event capturing
Related videos:JS Abstract Class and Event Design Pattern Video Tutorial
The above is the detailed content of [JavaScript] Two completely opposite event flows: event bubbling and event capturing. 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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...
