1. Event Processing ModelEvent bubbling and event capturing : Event bubbling and event capturing were proposed by Microsoft and Netscape respectively. Both concepts are intended to solve the problem of event flow (the order in which events occur) in the page. <div id="d1"> <div id="d2"> <div id="d3"></div> </div> </div> Given three nested divs, when the same event is registered for the three elements, what is their triggering order? 1. Event bubblingMicrosoft proposed an event stream called event bubbling. Elements that are structurally (not visually) nested will have a bubbling function, that is, the same event will bubble from the child element to the parent element. (Bottom-up) For the above example, if the bubbling method is used, the triggering order should be: d3——>d2——>d1, so let's verify it: (1) Bind events to three div elements//1. Get the element var d1 = document.querySelector('#d1') var d2 = document.querySelector('#d2') var d3 = document.querySelector('#d3') //2. Binding event d1.onclick = function(){ console.log(this.id) } d2.onclick = function(){ console.log(this.id) } d3.onclick = function(){ console.log(this.id) } (2) Operation results:Click on the red area: Click on the purple area: Click on the green area: The above is the event bubbling! 2. Event CaptureElements that are structurally (not visually) nested will have the function of event capture, that is, the same event is captured from the parent element to the child element (event source element). (Top-down) (ie no events are captured) For the above example, if the bubbling method is used, the triggering order should be: d1——>d2——>d3, so let's verify it: (1) Bind events to three div elements//1. Get the element var d1 = document.querySelector('#d1') var d2 = document.querySelector('#d2') var d3 = document.querySelector('#d3') //2. Binding event d1.onclick = function(){ console.log(this.id) } d2.onclick = function(){ console.log(this.id) } d3.onclick = function(){ console.log(this.id) } (2) Operation results:Click on the red area: Click on the purple area: Click on the green area: Event capture get!!! Notice:
2. Prevent event bubbling(1) W3C standard event.stopPropagation(); but IE9 and below do not support it//1. Get the element var d1 = document.querySelector('#d1') var d2 = document.querySelector('#d2') var d3 = document.querySelector('#d3') //2. Binding event d1.onclick = function(){ console.log(this.id) } d2.onclick = function(){ console.log(this.id) } d3.onclick = function(e){ e.stopPropagation(); console.log(this.id) } You will find that when you click the green area, no external events are triggered in sequence, and event bubbling is blocked: (2) Unique to IE: event.cancelBubble = true;//1. Get the element var d1 = document.querySelector('#d1') var d2 = document.querySelector('#d2') var d3 = document.querySelector('#d3') //2. Binding event d1.onclick = function(){ console.log(this.id) } d2.onclick = function(){ console.log(this.id) } d3.onclick = function(e){ e.cancelBubble = true; console.log(this.id) } The result is the same as (1). (3) Merge cancellation: return false In javascript, SummarizeThis article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: Detailed explanation of four solutions to floating problems in CSS layout
>>: Teach you how to deploy zabbix service on saltstack
Generally speaking, once a column in a data table...
The purpose of using HTML to mark up content is t...
Preface This article mainly shares with you an ex...
In HTML pages, visual elements such as buttons an...
Most navigation bars are arranged horizontally as...
Table of contents 1. Replace the apply method, ge...
In the front-end layout of the form, we often nee...
Download Nginx image in Docker docker pull nginx ...
Shtml and asp are similar. In files named shtml, s...
This article shares with you how to use Vue to im...
Table of contents 1. Overview 2. Download the Ngi...
The <input> tag The <input> tag is us...
Conversion between rgba and filter values under...
Purchase Certificate You can purchase it from Ali...
SQL fuzzy query statement The general fuzzy state...