Home Web Front-end JS Tutorial Optimizing page interaction experience: practical tips for event bubbling and event capturing

Optimizing page interaction experience: practical tips for event bubbling and event capturing

Jan 13, 2024 am 11:35 AM
Event bubbling Page optimization event capture

Optimizing page interaction experience: practical tips for event bubbling and event capturing

How to use event bubbling and event capturing to optimize page interaction experience

In web development, event bubbling and event capturing are two common event propagation mechanisms. They allow us to better handle interactive behaviors on the page and improve user experience. This article will introduce how to use event bubbling and event capturing to optimize page interaction, and give specific code examples.

1. Event bubbling

Event bubbling means that when an event (such as a click event) occurs on an element, this event will propagate to the upper element until it is propagated to the document object. . Through event bubbling, we can easily delegate events to multiple elements, simplify code writing and processing, and improve performance.

The following is a sample code for event bubbling:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>事件冒泡示例</title>
</head>
<body>
    <div id="container">
        <button id="btn1">按钮1</button>
        <button id="btn2">按钮2</button>
        <button id="btn3">按钮3</button>
    </div>

    <script>
        document.getElementById('container').addEventListener('click', function(event) {
            if(event.target.tagName === 'BUTTON') {
                console.log('点击了按钮:' + event.target.innerText);
            }
        });
    </script>
</body>
</html>
Copy after login

In the above code, we add the container element <div id="container"> A click event listener is created. When any button in the container is clicked, the event will bubble up to the container element and execute the code in the listener. By determining whether the target element of the event is a button, we can handle the button click event accordingly without adding a listener to each button, which greatly simplifies the code.

2. Event Capture

Event capture is the opposite of event bubbling. It starts from the document object and propagates to the specific target element. Through event capture, we can perform some special processing on the event before it reaches the target element, thereby better controlling the interactive effects and feedback of the event.

The following is a sample code for event capture:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>事件捕获示例</title>
</head>
<body>
    <div id="container">
        <button id="btn1">按钮1</button>
        <button id="btn2">按钮2</button>
        <button id="btn3">按钮3</button>
    </div>

    <script>
        document.getElementById('container').addEventListener('click', function(event) {
            if(event.target.tagName === 'BUTTON') {
                event.stopPropagation();   // 阻止事件冒泡
                console.log('点击了按钮:' + event.target.innerText);
            }
        }, true);
    </script>
</body>
</html>
Copy after login

In the above code, we added the container element <div id="container"> A click event listener, and set the event listener parameter useCapture to true to enable event capture. When any button in the container is clicked, the event will first be propagated to the container element and the code in the listener will be executed. By event.stopPropagation() to prevent event bubbling, we can only process the click event of the target element without affecting the event propagation of other elements.

Conclusion

By making reasonable use of event bubbling and event capturing, we can optimize the page interaction experience, simplify the code writing and processing process, and improve performance and user experience. Whether it is event delegation or event interception, they need to be used flexibly and event propagation must be handled carefully to avoid potential problems. I hope the sample code and instructions in this article are helpful to you.

The above is the detailed content of Optimizing page interaction experience: practical tips for event bubbling and event capturing. 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Understand the event bubbling mechanism: Why does a click on a child element affect the event of the parent element? Understand the event bubbling mechanism: Why does a click on a child element affect the event of the parent element? Jan 13, 2024 pm 02:55 PM

Understanding event bubbling: Why does a click on a child element trigger an event on the parent element? Event bubbling means that in a nested element structure, when a child element triggers an event, the event will be passed to the parent element layer by layer like bubbling, until the outermost parent element. This mechanism allows events on child elements to be propagated throughout the element tree and trigger all related elements in turn. To better understand event bubbling, let's look at a specific example code. HTML code: <divid="parent&q

Why can't click events in js be executed repeatedly? Why can't click events in js be executed repeatedly? May 07, 2024 pm 06:36 PM

Click events in JavaScript cannot be executed repeatedly because of the event bubbling mechanism. To solve this problem, you can take the following measures: Use event capture: Specify an event listener to fire before the event bubbles up. Handing over events: Use event.stopPropagation() to stop event bubbling. Use a timer: trigger the event listener again after some time.

Why does event bubbling trigger twice? Why does event bubbling trigger twice? Feb 22, 2024 am 09:06 AM

Why does event bubbling trigger twice? Event bubbling (Event Bubbling) means that in the DOM, when an element triggers an event (such as a click event), the event will bubble up from the element to the parent element until it bubbles to the top-level document object. . Event bubbling is part of the DOM event model, which allows developers to bind event listeners to parent elements, so that when child elements trigger events, the events can be captured and processed through the bubbling mechanism. However, sometimes developers encounter events that bubble up and trigger twice.

Reasons and solutions for jQuery .val() failure Reasons and solutions for jQuery .val() failure Feb 20, 2024 am 09:06 AM

Title: Reasons and solutions for the failure of jQuery.val() In front-end development, jQuery is often used to operate DOM elements. The .val() method is widely used to obtain and set the value of form elements. However, sometimes we encounter situations where the .val() method fails, resulting in the inability to correctly obtain or set the value of the form element. This article will explore the causes of .val() failure, provide corresponding solutions, and attach specific code examples. 1.Cause analysis.val() method

What scenarios can event modifiers in vue be used for? What scenarios can event modifiers in vue be used for? May 09, 2024 pm 02:33 PM

Vue.js event modifiers are used to add specific behaviors, including: preventing default behavior (.prevent) stopping event bubbling (.stop) one-time event (.once) capturing event (.capture) passive event listening (.passive) Adaptive modifier (.self)Key modifier (.key)

What is event bubbling? In-depth analysis of event bubbling mechanism What is event bubbling? In-depth analysis of event bubbling mechanism Feb 20, 2024 pm 05:27 PM

What is event bubbling? In-depth analysis of the event bubbling mechanism Event bubbling is an important concept in web development, which defines the way events are delivered on the page. When an event on an element is triggered, the event will be transmitted starting from the innermost element and passed outwards until it is passed to the outermost element. This delivery method is like bubbles bubbling in water, so it is called event bubbling. In this article, we will analyze the event bubbling mechanism in depth. The principle of event bubbling can be understood through a simple example. Suppose we have an H

What are the JS events that don't bubble up? What are the JS events that don't bubble up? Feb 18, 2024 pm 06:31 PM

What are the JS events that will not bubble up? JavaScript is a powerful scripting language that adds interactivity and dynamics to web pages. In JavaScript, event-driven programming is a very important part. Events refer to various operations performed by users on web pages, such as button clicks, mouse movements, keyboard input, etc. JavaScript responds to these events through event handling functions and performs corresponding operations. Event bubbling is a common mechanism during event processing. Event bubbling means that when an

Which JS events don't bubble up? Which JS events don't bubble up? Feb 19, 2024 pm 09:56 PM

What are the situations in JS events that will not bubble up? Event bubbling (Event Bubbling) means that after an event is triggered on an element, the event will be transmitted upward along the DOM tree starting from the innermost element to the outermost element. This method of transmission is called event bubbling. However, not all events can bubble up. There are some special cases where events will not bubble up. This article will introduce the situations in JavaScript where events will not bubble up. 1. Use stopPropagati

See all articles