Home  >  Article  >  Web Front-end  >  Learn the principles of click event bubbling and how to use it in web development

Learn the principles of click event bubbling and how to use it in web development

王林
王林Original
2024-01-13 12:47:211120browse

Learn the principles of click event bubbling and how to use it in web development

Understand the principle of click event bubbling and its application in web development

In web development, interaction with users is often involved. Among them, events are one of the important mechanisms to achieve this interactive effect. Among these events, click event is the most widely used one. Learning to understand the principle of click event bubbling and its application in web development can better grasp the event mechanism and achieve a richer user interaction experience.

1. The principle of click event bubbling

When an event occurs on an element, if the element has a parent element, and the parent element is also bound to the same type of event, Then the event will bubble up from the child elements to the top-level parent element. This process is called event bubbling.

For example, there is a web page with the following HTML structure:

<div id="box">
  <button id="btn">点击</button>
</div>

Assume that a click event listener is bound to this button:

document.getElementById("btn").addEventListener("click", function(){
  console.log("按钮被点击了");
});

When the button is clicked , the console will output "The button was clicked". This is because the button's click event triggered the listener.

If we bind an event listener of the same type to the parent element div:

document.getElementById("box").addEventListener("click", function(){
  console.log("div被点击了");
});

In this way, when the button is clicked, not only "the button was clicked" is output, but also "div was clicked" will be output. This is because after the click event is triggered on the button, it will continue to bubble up to the parent element div.

2. Application of click event bubbling

  1. Improve the maintainability and scalability of the code

By clicking event bubbling, we You can bind an event listener to the parent element without binding an event listener to each child element. This can greatly reduce the amount of code and facilitate maintenance and expansion.

For example, suppose there is a ul list with multiple li elements in it. To achieve when a li element is clicked, its background color is changed. We can write the code like this:

<ul id="list">
  <li>选项1</li>
  <li>选项2</li>
  <li>选项3</li>
</ul>
var lis = document.getElementById("list").getElementsByTagName("li");
for(var i=0; i<lis.length; i++){
  lis[i].addEventListener("click", function(){
    this.style.backgroundColor = "red";
  });
}

However, if we need to add new li elements later, we need to maintain it again in the JavaScript code. And if we use event bubbling, we only need to bind an event listener to the ul element:

document.getElementById("list").addEventListener("click", function(e){
  if(e.target.tagName.toLowerCase() === "li"){
    e.target.style.backgroundColor = "red";
  }
});

No matter how many li elements there are, we only need one listener and can use the event bubbling mechanism. Capture the event on the parent element, and then determine which child element was clicked based on the event source.

  1. Implement event delegation

By using event bubbling, we can realize the function of event delegation. Event delegation refers to handing over an element's events to its parent element or a higher-level element for processing. This reduces the number of listeners and facilitates dynamic binding.

For example, suppose we have a table, and when the mouse hovers over a cell, the background color of the cell changes. We can write the code like this:

<table id="table">
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
  </tr>
  <tr>
    <td>4</td>
    <td>5</td>
    <td>6</td>
  </tr>
</table>
var tds = document.getElementById("table").getElementsByTagName("td");
for(var i=0; i<tds.length; i++){
  tds[i].addEventListener("mouseover", function(){
    this.style.backgroundColor = "yellow";
  });
  tds[i].addEventListener("mouseout", function(){
    this.style.backgroundColor = "white";
  });
}

However, if we need to add new cells later, we need to maintain it again in the JavaScript code. If we use event bubbling, we only need to bind an event listener to the table element:

document.getElementById("table").addEventListener("mouseover", function(e){
  if(e.target.tagName.toLowerCase() === "td"){
    e.target.style.backgroundColor = "yellow";
  }
});
document.getElementById("table").addEventListener("mouseout", function(e){
  if(e.target.tagName.toLowerCase() === "td"){
    e.target.style.backgroundColor = "white";
  }
});

By judging the event source, we can avoid binding a listener to each cell. This reduces the number of listeners and facilitates dynamic binding.

In short, understanding the principle of click event bubbling and its application in web development can improve the maintainability and scalability of the code, and at the same time realize the function of event delegation. At the same time, event bubbling can also better control and handle user interactions. In actual web development, an in-depth understanding and flexible use of the click event bubbling mechanism will greatly improve development efficiency and user experience.

The above is the detailed content of Learn the principles of click event bubbling and how to use it in web development. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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