Home Web Front-end JS Tutorial No support for bubbling events: limitations and scope

No support for bubbling events: limitations and scope

Jan 13, 2024 pm 12:51 PM
bubbling event limitation Bubbling is not supported

No support for bubbling events: limitations and scope

Bubbling Event refers to an event delivery method that is triggered step by step from child elements to parent elements in the DOM tree. In most cases, bubbling events are very flexible and scalable, but there are some special cases where events do not support bubbling.

1. Which events do not support bubbling?
Although most events support bubbling, there are some events that do not support bubbling. The following are some common events that do not support bubbling:

  1. focus and blur events
  2. load and unload events
  3. input, select and change events
  4. submit and reset event
  5. scroll event
  6. mouseenter and mouseleave event
  7. contextmenu event

2. Event example
For To better understand the limitations of bubbling events, specific code examples are given below for each event that does not support bubbling for a better understanding:

  1. focus and blur events
    The focus and blur events are events used to handle elements gaining or losing focus. These events do not support bubbling, which means that when you trigger a focus or blur event on a child element, the corresponding event on the parent element will not be triggered.

HTML code:

1

2

3

<div>

  <input type="text" id="myInput">

</div>

Copy after login

JavaScript code:

1

2

3

4

5

6

7

8

const myInput = document.getElementById('myInput');

myInput.addEventListener('focus', function() {

  console.log('Input获得焦点');

});

const myDiv = document.querySelector('div');

myDiv.addEventListener('focus', function() {

  console.log('Div获得焦点');

});

Copy after login

Result:
When the text box gets focus, only "Input obtained" will be output in the console Focus" instead of outputting "Div has focus". Because the focus event does not bubble up to the parent element div.

  1. Load and unload events
    The load and unload events are events triggered after the page or resource is loaded. These events do not support bubbling, which means that when a load or unload event is triggered on a child element, the corresponding event on the parent element will not be triggered.

HTML code:

1

2

3

<div>

  <img  src="/static/imghw/default1.png"  data-src="image.png"  class="lazy" id="myImage" alt="No support for bubbling events: limitations and scope" >

</div>

Copy after login

JavaScript code:

1

2

3

4

5

6

7

8

const myImage = document.getElementById('myImage');

myImage.addEventListener('load', function() {

  console.log('图片加载完成');

});

const myDiv = document.querySelector('div');

myDiv.addEventListener('load', function() {

  console.log('Div加载完成');

});

Copy after login

Result:
When the image loading is completed, only "Image loading completed" will be output on the console " instead of outputting "Div loading completed". Because the load event does not bubble up to the parent element div.

  1. Input, select and change events
    Input, select and change events are events used to handle changes in form element values. These events only affect the element whose value actually changed and will not bubble up to the parent element.

HTML code:

1

<input type="text" id="myInput">

Copy after login

JavaScript code:

1

2

3

4

5

6

7

8

const myInput = document.getElementById('myInput');

myInput.addEventListener('input', function() {

  console.log('输入框值改变');

});

const myForm = document.querySelector('form');

myForm.addEventListener('input', function() {

  console.log('表单值改变');

});

Copy after login

Result:
When the value of the input box changes, only "Input" will be output on the console Box value changed" instead of "Form value changed" will not be output. Because the input event does not bubble up to the parent element form.

  1. submit and reset events
    The submit and reset events are events that are triggered when the form is submitted and reset. These events can only be applied to the form element itself and will not bubble up to parent elements.

HTML code:

1

2

3

<form id="myForm">

  <input type="submit" value="提交">

</form>

Copy after login

JavaScript code:

1

2

3

4

5

6

7

8

9

const myForm = document.getElementById('myForm');

myForm.addEventListener('submit', function(event) {

  event.preventDefault();

  console.log('表单已提交');

});

const myDiv = document.querySelector('div');

myDiv.addEventListener('submit', function() {

  console.log('Div提交');

});

Copy after login

Result:
When the submit button is clicked, only "Form submitted" will be output in the console " instead of "Div Submit" will be output. Because the submit event does not bubble up to the parent element div. Note that in the example we prevent the form's default submission behavior through the event.preventDefault() method.

  1. scroll event
    scroll event is an event triggered when scrolling occurs. This event does not support bubbling, which means that when scrolling an element, the scroll event on the parent element will not be triggered.

HTML code:

1

2

3

<div style="height: 100px; width: 100px; overflow: auto;">

  <p>这是一段很长的文本</p>

</div>

Copy after login

JavaScript code:

1

2

3

4

const myDiv = document.querySelector('div');

myDiv.addEventListener('scroll', function() {

  console.log('滚动');

});

Copy after login

Result:
When the div is scrolled, only "scroll" will be output in the console, and Will not bubble to upper elements.

  1. mouseenter and mouseleave events
    The mouseenter and mouseleave events are events that are triggered when the mouse enters and leaves an element. These events do not support bubbling, which means that when the mouse enters or leaves an element, the corresponding event on the parent element will not be triggered.

HTML code:

1

2

3

<div id="myDiv" style="background-color: yellow; width: 100px; height: 100px;">

  <p>鼠标进入这个div</p>

</div>

Copy after login

JavaScript code:

1

2

3

4

5

6

7

8

const myDiv = document.getElementById('myDiv');

myDiv.addEventListener('mouseenter', function() {

  console.log('鼠标进入div');

});

const myBody = document.querySelector('body');

myBody.addEventListener('mouseenter', function() {

  console.log('鼠标进入body');

});

Copy after login

Result:
When the mouse enters the div, only "The mouse enters the div" will be output in the console ", instead of outputting "mouse enters body".

  1. contextmenu event
    The contextmenu event is an event triggered when the mouse right button is clicked. This event does not support bubbling, which means that when you right-click an element, the contextmenu event on the parent element will not be triggered.

HTML code:

1

<div id="myDiv" style="background-color: yellow; width: 100px; height: 100px;"></div>

Copy after login

JavaScript code:

1

2

3

4

5

6

7

8

9

const myDiv = document.getElementById('myDiv');

myDiv.addEventListener('contextmenu', function(event) {

  event.preventDefault();

  console.log('右键点击');

});

const myBody = document.querySelector('body');

myBody.addEventListener('contextmenu', function() {

  console.log('右键点击body');

});

Copy after login

Result:
When you right-click a div, only "right-click" will be output in the console , instead of outputting "right click on body". Note that in the example we prevent the default context menu from being displayed through the event.preventDefault() method.

3. Summary
Bubble events are an event delivery method that is triggered step by step from child elements to parent elements in the DOM tree. Most events support bubbling, but there are also some that do not. Bubble special events. This article analyzes events that do not support bubbling through specific code examples, hoping to help readers understand the limitations of bubbling events.

The above is the detailed content of No support for bubbling events: limitations and scope. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the common ways to prevent bubbling events? What are the common ways to prevent bubbling events? Feb 19, 2024 pm 10:25 PM

What are the commonly used commands to prevent bubbling events? In web development, we often encounter situations where we need to handle event bubbling. When an event is triggered on an element, such as a click event, its parent element will also trigger the same event. This behavior of event delivery is called event bubbling. Sometimes, we want to prevent an event from bubbling up, so that the event only fires on the current element, and prevents it from being passed to superior elements. To achieve this, we can use some common directives that prevent bubbling events. event.stopPropa

Analyze the advantages and disadvantages of static positioning technology Analyze the advantages and disadvantages of static positioning technology Jan 18, 2024 am 11:16 AM

Analysis of the advantages and limitations of static positioning technology With the development of modern technology, positioning technology has become an indispensable part of our lives. As one of them, static positioning technology has its unique advantages and limitations. This article will conduct an in-depth analysis of static positioning technology to better understand its current application status and future development trends. First, let’s take a look at the advantages of static positioning technology. Static positioning technology achieves the determination of position information by observing, measuring and calculating the object to be positioned. Compared with other positioning technologies,

What events cannot bubble up? What events cannot bubble up? Nov 20, 2023 pm 03:00 PM

The events that cannot bubble are: 1. focus event; 2. blur event; 3. scroll event; 4. mouseenter and mouseleave events; 5. mouseover and mouseout events; 6. mousemove event; 7. keypress event; 8. beforeunload event ; 9. DOMContentLoaded event; 10. cut, copy and paste events, etc.

No support for bubbling events: limitations and scope No support for bubbling events: limitations and scope Jan 13, 2024 pm 12:51 PM

Bubbling event (BubblingEvent) refers to an event delivery method that is triggered step by step from child elements to parent elements in the DOM tree. In most cases, bubbling events are very flexible and scalable, but there are some special cases where events do not support bubbling. 1. Which events do not support bubbling? Although most events support bubbling, there are some events that do not support bubbling. The following are some common events that do not support bubbling: focus and blur events load and unloa

What is the meaning of bubbling events What is the meaning of bubbling events Feb 19, 2024 am 11:53 AM

Bubbling events mean that in web development, when an event is triggered on an element, the event will propagate to upper elements until it reaches the document root element. This propagation method is like a bubble gradually rising from the bottom, so it is called a bubbling event. In actual development, knowing and understanding how bubbling events work is very important to handle events correctly. The following will introduce the concept and usage of bubbling events in detail through specific code examples. First, we create a simple HTML page with a parent element and three children

What are the instructions to prevent bubbling events? What are the instructions to prevent bubbling events? Nov 21, 2023 pm 04:14 PM

Instructions to prevent bubbling events include stopPropagation(), cancelBubble attribute, event.stopPropagation(), event.cancelBubble attribute, event.stopImmediatePropagation(), etc. Detailed introduction: 1. stopPropagation() is one of the most commonly used instructions, used to stop the propagation of events. When an event is triggered, calling this method can prevent the event from continuing, etc.

Master the common event bubbling mechanism in JavaScript Master the common event bubbling mechanism in JavaScript Feb 19, 2024 pm 04:43 PM

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

How to effectively prevent bubbling events? Command analysis! How to effectively prevent bubbling events? Command analysis! Feb 23, 2024 am 11:33 AM

How to effectively prevent bubbling events? Command analysis! A bubbling event means that an object triggers an event during program execution, and the event will bubble up and pass to the parent element of the object until it is processed or reaches the top level of the document. Bubbling events may cause unnecessary code execution or page operations, affecting user experience. Therefore, we need to take some measures to effectively prevent the spread of bubbling events. Here are some instructions that can be used to stop the propagation of bubbling events: Use event.stopPropagation

See all articles