Home > Web Front-end > JS Tutorial > body text

Practical tips and case studies for preventing incidents from bubbling up

王林
Release: 2024-01-13 15:28:16
Original
1372 people have browsed it

Practical tips and case studies for preventing incidents from bubbling up

Practical tips and case analysis to prevent event bubbling

Event bubbling means that in the DOM tree, when an element triggers an event, the event will Bubbles up to the parent element in the DOM tree, all the way to the root node. This bubbling mechanism can sometimes lead to unexpected problems and errors. In order to avoid this problem from happening, we need to learn to use some practical techniques to prevent events from bubbling up. This article will introduce some commonly used techniques to prevent event bubbling, analyze them with cases, and provide specific code examples.

1. Use the stopPropagation method of the event object

In JavaScript, the event object (event) provides a stopPropagation method, which can be used to prevent the event from continuing to bubble. The use of this method is very simple, just call event.stopPropagation() in the event processing function.

For example, in a page with a button and a parent element for click events, when the button is clicked, prevent the click event from bubbling up to the parent element:

<div id="parent">
  <button id="btn">点击按钮</button>
</div>

<script>
  const parent = document.getElementById('parent');
  const btn = document.getElementById('btn');

  btn.addEventListener('click', function(event) {
    event.stopPropagation();
    console.log('点击了按钮');
  });

  parent.addEventListener('click', function() {
    console.log('点击了父元素');
  });
</script>
Copy after login

In the above code, click After pressing the button, only "the button was clicked" was output on the console, but "the parent element was clicked" was not triggered, indicating that the event bubbling was successfully prevented by calling the stopPropagation method.

2. Use the cancelBubble property of the event object

In addition to using the stopPropagation method, the event object also provides a cancelBubble property, which is a Boolean value. You can prevent events from bubbling by setting the cancelBubble property to true.

For example, in a page with a link and a parent element for click events, when the link is clicked, prevent the click event from bubbling up to the parent element:

<div id="parent">
  <a href="#" id="link">点击链接</a>
</div>

<script>
  const parent = document.getElementById('parent');
  const link = document.getElementById('link');

  link.addEventListener('click', function(event) {
    event.cancelBubble = true;
    console.log('点击了链接');
  });

  parent.addEventListener('click', function() {
    console.log('点击了父元素');
  });
</script>
Copy after login

In the above code, click After the link, only "the link was clicked" was output on the console, but "the parent element was clicked" was not triggered, indicating that the event bubbling was successfully prevented by setting the cancelBubble attribute to true.

3. Case Analysis

Now we use a specific case to further analyze the scenarios and techniques for preventing event bubbling.

Suppose there are multiple nested div elements in a page, and each div element has its own click event. Now, what we want to achieve is that when the innermost div is clicked, only the click event of the div is triggered, but not the click event of its parent element.

<div id="outer" style="background: yellow;">
  <div id="middle" style="background: blue;">
    <div id="inner" style="background: red;"></div>
  </div>
</div>

<script>
  const outer = document.getElementById('outer');
  const middle = document.getElementById('middle');
  const inner = document.getElementById('inner');

  outer.addEventListener('click', function() {
    console.log('点击了外层div');
  });

  middle.addEventListener('click', function() {
    console.log('点击了中间div');
  });

  inner.addEventListener('click', function(event) {
    event.stopPropagation();
    console.log('点击了最内层div');
  });
</script>
Copy after login

In the above code, when we click on the innermost div, the only output in the console is "clicked on the innermost div", while "clicked on the middle div" and "clicked on the outer layer div" was not triggered, indicating that the event bubbling was successfully prevented by calling the stopPropagation method, achieving our needs.

In summary, by using the stopPropagation method or cancelBubble property of the event object, you can easily prevent event bubbling. In actual development, it is important to choose appropriate techniques to prevent event bubbling according to specific needs. Hopefully this article will help you better apply techniques for preventing event bubbling.

The above is the detailed content of Practical tips and case studies for preventing incidents from bubbling up. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template