Comparative analysis of event capture, bubbling and event delegation
What is the relationship between the three?
1. Event capturing and bubbling are two different stages of executing events in modern browsers
2. Event delegation uses bubbling Stage operation mechanism to realize
Event bubbling and capturing operation diagram
运行条件:当一个事件发生在具有父元素的的元素上时,现代浏览器根据事件添加时的设置来执行(冒泡或者捕获)
Use the third attribute of addEventListener() to set whether the event is registered through the capture phase (true) or the bubbling phase (false). The default is false.
Event bubbling
The execution proceeds from the actual element (event) to the upper parent element level by level until it reaches
有些时候父元素和子元素都定义了click事件,但是不希望点击子元素的时候执行父元素的click事件(例如dialog弹窗的遮罩层如果是父元素,而dialog弹窗内容层是子元素,同时可以通过点击遮罩层来关闭弹窗,但是点击内容层不关闭弹窗),可以通过stopPropagation()在子元素上阻止冒泡。
Event capture ( Generally not used)
The browser checks the element's outermost ancestor to see if an onclick event handler is registered in the capture phase, and if so, runs it.
Then it moves to the next element in (the parent of the clicked element) and does the same thing, then the next element (the parent of the clicked element), and so on, until the actual clicked element is reached.
The difference between event capturing and bubbling
The difference in execution order
Bubbling:
Capture:
Implementation method on in jqueryRelated recommendations:
About event bubbling and event capture mechanism in javascript
Events Detailed explanation of bubbling and event capture examples
Event capture, event bubbling and event delegation mechanism in Javascript
The above is the detailed content of Comparative analysis of event capture, bubbling and event delegation. 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

Common bubbling events in JavaScript: To master the bubbling characteristics of common events, specific code examples are required. Introduction: In JavaScript, event bubbling means that the event will start from the element with the deepest nesting level and propagate to the outer element until it propagates to The outermost parent element. Understanding and mastering common bubbling events can help us better handle user interaction and event handling. This article will introduce some common bubbling events and provide specific code examples to help readers better understand. 1. Click event (click

Which JS events will not bubble? In JavaScript, event bubbling means that when an element triggers an event, the event will bubble up to higher-level elements until it bubbles to the document root node. The event handlers are then executed in the order they bubble up. However, not all events bubble up. Some events will only execute the event handler on the target element after being triggered, without bubbling up to higher-level elements. Here are some common events that do not bubble: focus and blur events:

Why does the same bubbling event happen twice? Event bubbling is a common event delivery mechanism in browsers. When an element triggers an event, the event will be passed from the triggered element to the upper elements in sequence until it is passed to the root element of the document. This process is like bubbles bubbling in water, so it is called event bubbling. However, sometimes we find that the same bubbling event occurs twice. Why is this? There are two main reasons: event registration and event processing. First, we need to make it clear that the event

This guide will show you what you have to do to take a screenshot on your MacBook Pro. MacBooks are known for their sleek design and powerful performance, but these powerful laptops come with a number of convenient features that are often overlooked. One of the features is a built-in tool for capturing screenshots. This article will guide you step by step on how to take a screenshot on MacBook Pro, whether you want to capture the entire screen or just a specific part of the screen. What is a screenshot? A screenshot, also called a screenshot, is a digital image taken by a computer or mobile device to record an item visible on the screen. Screenshots are typically used to create a record of images or text that you can't easily save as a file, share a screen view with others, or create guides and tutorials, like

Capture first or bubble first? Analyzing the advantages and disadvantages of event process Event process is an important concept in web development. It describes the process of events from occurrence to processing. There are two main process models when handling events: capture then bubble and bubble then capture. These two models have their own advantages and disadvantages in different scenarios, and you need to choose the appropriate model based on the actual situation. Capturing first and then bubbling means that the event capturing phase is executed before the event bubbling phase. The event capture phase starts from the root node of the event target and passes down step by step until it reaches the target element.

The role of click event bubbling and its impact on web page interaction In web development, events are the key to realizing interaction and responding to user operations. Among them, event bubbling is a common event mechanism that allows events in a nested element hierarchy to be responded to by multiple elements at the same time. This article will explain in detail the role of click event bubbling, its impact on web page interaction, and provide some specific code examples. 1. The concept of click event bubbling Click event bubbling (ClickEvent Bubbling) refers to when an element

Exception handling: How to catch and handle exceptions in PHP? In PHP development, exception handling is a very important part. When an unexpected situation or error occurs in the program, we need to ensure the normal operation of the program by catching and handling exceptions. PHP provides a set of exception handling mechanisms. This article will introduce how to catch and handle exceptions in PHP and provide specific code examples. 1. The basic concept of exceptions in PHP. In PHP, an exception refers to an abnormal situation that occurs during the running of the program, such as errors, warnings, and fatal errors.

Taking a Screenshot on Windows The easiest way to take a screenshot in Windows is to use the Print Screen (PrtScn) key on your keyboard. This key is usually located on the right side of the keyboard layout and is the most direct way to create a screenshot. After pressing the "PrtScn" button on your Windows desktop or laptop keyboard, you will enter screenshot capture mode, the screen will dim and a small menu will appear in the top center of the screen as shown in the image below. Here are the steps you can follow: Press the "PrtScn" key. Open an image editing tool such as Paint or Photoshop. Press "Ctrl+V" to paste the screenshot. Edit as needed, then save the screenshot. However, PrtScn
