This article mainly introduces relevant information on the detailed explanation of event delegation in javascript. Friends who need it can refer to it
I have seen an interview question these days, which is probably, asking you to give 1,000 li How should I add a click event? Most people's first impression may be to add one to each li. If this is the case, it is estimated that they will be GG during the interview. Here we have withdrawn our Event bubbling and capturing mechanisms, as well as event delegation mechanisms, let’s take a look at the above.
First let’s talk about event bubbling and event capturing mechanisms. Event bubbling was proposed by Microsoft. Event capture was proposed by Netscape. At that time, the two companies were at loggerheads. Later, W3C had no choice but to adopt a compromise method. When an event occurs, it is captured first and then bubbles up.
Usually , there are three ways to listen to events in js, namely:
ele.addEventListener(type,listener,[useCapture]);//IE6~8 does not support
ele.attachEvent('on' type,listener);//Supported by IE6~10, not supported by IE11
## ele.onClick=function(){}; //All browsers support
The w3c specification defines three event stages, which are the capture stage, the target stage, and the bubbling stage. In the dom2 level regulations specified by w3c, the addEventListener is used to listen for events. So we will use addEventListener to explain. First of all, bubbles are just like when you throw a stone into the water, the bubbles in the water will pop up from below, which means that after the event is triggered, it will move from the direction of the child element to the parent element. Trigger, while the capture mechanism is just the opposite. The capture mechanism triggers events from the parent element to the child element, and the third parameter in the addEventListener function is used to determine whether to use the capture mechanism or the bubbling mechanism. When useCapture is true It is a capturing mechanism. When useCapture is false, it is a bubbling mechanism. Let’s take a look at the example: Copy code<p class="parent"> <p class="child"> </p> </p> <script> var parent = document.getElementsByClassName('parent')[0]; var child = document.getElementsByClassName('child')[0]; parent.addEventListener('click',function(){ console.log("这里是父元素"); },false); child.addEventListener('click',function(){ console.log("这里是子元素"); },false); </script>
<ul id="parent-list"> <li id="post-1">Item 1</li> <li id="post-2">Item 2</li> <li id="post-3">Item 3</li> <li id="post-4">Item 4</li> <li id="post-5">Item 5</li> <li id="post-6">Item 6</li> </ul>
// 找到父元素,绑定一个监听事件 document.getElementById("parent-list").addEventListener("click", function(e) { // e.target是点击的元素 // 如果它是li元素 if(e.target && e.target.nodeName == "LI") { // console.log("List item ", e.target.id.replace("post-", ""), " was clicked!"); } });
document.getElementById("myp").addEventListener("click",function(e) { // e.target 就是当前被点击的元素 if (e.target && e.target.matches("a.classA")) { console.log("Anchor element clicked!"); } });
Vue.jsDetailed explanation of the steps to develop mpvue framework
Loading and removal jsDetailed explanation of steps with css files
How to write JS when you need to traverse irregular multi-dimensional arrays
The above is the detailed content of Event delegation in javascript (picture and text tutorial). For more information, please follow other related articles on the PHP Chinese website!