JS bubbling event example analysis: To master how to use bubbling events to achieve interactive effects, specific code examples are needed
With the development of the Internet, JavaScript (JS) has become An important technology in front-end development. In front-end development, JS bubbling events are often used to achieve interactive effects. This article will introduce the basic concepts and usage of JS bubbling events through specific code examples, and analyze how to use bubbling events to achieve some common interactive effects.
1. Basic concepts of JS bubbling events
In HTML documents, the element structure is often a nested relationship. When an element triggers an event, the event is propagated (bubbled) up to the parent element until it reaches the document root element. This is the basic concept of JS bubbling events. Bubbling events include common click events, mouse move-in and move-out events, keyboard events, etc.
2. Specific usage of bubbling events
元素1元素2元素3
var container = document.getElementById('container'); var elements = container.getElementsByTagName('div'); // 为每个元素绑定事件监听器 for (var i = 0; i < elements.length; i++) { elements[i].addEventListener('click', function(event){ console.log('当前元素:', event.target.innerHTML); console.log('冒泡元素:', getBubblingElements(event)); }); } // 递归获取所有冒泡元素 function getBubblingElements(event) { var bubblingElements = []; var currentElement = event.target; while (currentElement !== document) { bubblingElements.push(currentElement.innerHTML); currentElement = currentElement.parentNode; } return bubblingElements; }
In the code, we first obtain the container element and its sub-elements, and then bind a click event listener to each sub-element. When a child element is clicked, the listener prints out the contents of the current element and all bubbled elements.
当前元素: 元素3 冒泡元素: [元素3, 元素2, 元素1, #container]
The above results indicate that the actual element clicked is element 3, and the bubbling process passes through element 2, element 1 and the container element in sequence. (#container).
3. Use bubbling events to achieve interactive effects
After mastering the basic concepts and specific usage of bubbling events, we can use bubbling events to achieve some common interactive effects. The following takes the cancellation of bubbling, event proxy and event delegation as an example for analysis.
// 为每个元素绑定事件监听器 for (var i = 0; i < elements.length; i++) { elements[i].addEventListener('click', function(event){ console.log('当前元素:', event.target.innerHTML); console.log('冒泡元素:', getBubblingElements(event)); event.stopPropagation(); // 取消事件冒泡 }); }
In the modified code, the stopPropagation method of the event object is added and called in the listener This method can cancel the bubbling delivery of events. In this way, when a div element is clicked, only the current element and its content will be output to the console.
container.addEventListener('click', function(event){ if (event.target.tagName === 'DIV') { // 只处理div元素的点击事件 console.log('当前元素:', event.target.innerHTML); console.log('冒泡元素:', getBubblingElements(event)); } });
In the modified code, bind the event listener to the container element and add the In the code, it is determined that the div element is clicked by judging the tagName of event.target. This way, no matter how many div elements we add to the container, we only need one event listener.
container.addEventListener('click', function(event){ if (event.target.tagName === 'DIV') { // 只处理div元素的点击事件 console.log('当前元素:', event.target.innerHTML); console.log('冒泡元素:', getBubblingElements(event)); } });
In the modified code, only a click is added for the container element Event listener, and determine whether the tagName of event.target is a div element in the listener code. In this way, no matter how many div elements we add to the container, they will be processed by the event listener proxy.
Summary:
This article introduces the basic concepts and usage of JS bubbling events through specific code examples, and analyzes in detail how to use bubbling events to achieve some common interactive effects, including canceling bubbling. , event proxy and event delegation. Mastering the use of bubbling events can improve the efficiency of interactive effects in front-end development and provide web users with a better user experience. I hope this article will help readers understand and apply JS bubbling events.
The above is the detailed content of Learn how to use bubbling events to achieve interactive effects: Example analysis of JS bubbling events. For more information, please follow other related articles on the PHP Chinese website!