The content of this article is an introduction to the knowledge of JavaScript bubbling and capturing (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
What is JS bubbling and capturing?
Bubbing and capturing refer to the two directions, or processes, in which js delivers events when an event on an element is triggered.
Foreword:
For example, there is such a page and js method
Less: I use less to write Yes, if there is no less environment, you can ignore this paragraph.
.level { padding: 50px 80px; } .level-template(@level: 1, @color: #fff){ background-color: darken( @color , 5% * @level); } #lv1{ .level-template(1)} #lv2{ .level-template(2)} #lv3{ .level-template(3)} #lv4{ .level-template(4)}
HTML
<div> <div> <div> <div> </div> </div> </div> </div>
JS
function $(id) { return document.getElementById(id); } window.onload = () => { $('lv1').addEventListener("click",()=>{console.log('lv1')},true); $('lv2').addEventListener("click",()=>{console.log('lv2')},true); $('lv3').addEventListener("click",()=>{console.log('lv3')},true); }; // 上面的 () => {} 为 ES6 的匿名方法的定义方式 // 等同于 function () { console.log('lv1') }
What the above js does:
When the page is loading, add click monitoring methods to the three divs, yourself When clicked, its own id value will be output in the console.
The structure of the page is like this: lv1 contains lv2, and lv2 contains lv3. When you click on lv3, you actually click on lv2 and lv1, because lv3 is inside lv2. When you click on lv3, you will naturally click on it. lv2 and lv1, that is to say, when lv3 is clicked, three click events will be triggered.
As for the order in which these three events are triggered, it is the so-called bubbling and capturing.
Three stages of event triggering:
-
Capture stage: First go from the root node of the document
document
to the event triggering object, Capture the event object from the outside in; - Locate the target: Find the target event location (where the incident occurred) and trigger the event;
- Bubble stage: Then trace back from the target event location to the root node of the document, and bubble the event object from the inside out.
1. Capture phase
As in the above example, when lv3 is clicked, js will start from the top layer of the document and search for the origin of the click event from the outside inward. That’s lv3. Then this process from the outside to the inside is lv1 --> lv2 --> lv3. The click events of these three p's are triggered in sequence according to this process.
The direction of this trigger is the direction of capture.
2. Bubbling phase
After finding the clicked lv3, the event is passed upward. The process is lv3 --> lv2 --> lv1. At this time, lv3 and lv2 are triggered in sequence. , lv1's click event, this inside-out triggering process is called bubbling
Let's look back at the format of the most commonly used event binding methods:
element.addEventListener(event, function, useCapture)
Here, useCapture is Boolean value used to define whether the event is triggered in the bubbling phase or in the capturing phase. The default value is false, which means it is triggered in the bubbling phase.
At this point you will know that the click method defined in the above example is executed in the capture phase, then the returned result is
lv1 lv2 lv3
If the top example, the onload content is like this
window.onload = () => { $('lv1').addEventListener("click",()=>{console.log('lv1')},false); $('lv2').addEventListener("click",()=>{console.log('lv2')},false); $('lv3').addEventListener("click",()=>{console.log('lv3')},false); };
So that is to say, the click
event is triggered in the bubbling stage
, and the returned result is
lv3 lv2 lv1
Summary
The relationship between bubbling and capturing will only appear in the containing and contained structures. This relationship will not exist in brother relationships.
Bubbling and capturing are just different in direction.
The above is the detailed content of Introduction to JavaScript bubbling and capturing (with code). For more information, please follow other related articles on the PHP Chinese website!

本篇文章带大家了解一下HTML(超文本标记语言),介绍一下HTML的本质,HTML文档的结构、HTML文档的基本标签和图像标签、列表、表格标签、媒体元素、表单,希望对大家有所帮助!

不算。html是一种用来告知浏览器如何组织页面的标记语言,而CSS是一种用来表现HTML或XML等文件样式的样式设计语言;html和css不具备很强的逻辑性和流程控制功能,缺乏灵活性,且html和css不能按照人类的设计对一件工作进行重复的循环,直至得到让人类满意的答案。

总结了一些web前端面试(笔试)题分享给大家,本篇文章就先给大家分享HTML部分的笔试题(附答案),大家可以自己做做,看看能答对几个!

HTML5中画布标签是“<canvas>”。canvas标签用于图形的绘制,它只是一个矩形的图形容器,绘制图形必须通过脚本(通常是JavaScript)来完成;开发者可利用多种js方法来在canvas中绘制路径、盒、圆、字符以及添加图像等。

html5废弃了dir列表标签。dir标签被用来定义目录列表,一般和li标签配合使用,在dir标签对中通过li标签来设置列表项,语法“<dir><li>列表项值</li>...</dir>”。HTML5已经不支持dir,可使用ul标签取代。

在html中,document是文档对象的意思,代表浏览器窗口的文档;document对象是window对象的子对象,所以可通过“window.document”属性对其进行访问,每个载入浏览器的HTML文档都会成为Document对象。

html5支持boolean值属性;boolean值属性指是属性值为true或者false的属性,如input元素中的disabled属性,不使用该属性表示值为flase,不禁用元素,使用该属性可以不设置属性值表示值为true,禁用元素。


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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
