Event capture, event bubbling, event delegation [practice]

Deepen understanding after practice

One: Event flow

Understanding of the next event flow chart

Insert picture description here

Two: event delegation

Principle of event delegation
  • When there are a large number of events of the same type, you can use the delivery process of the event stream to bind the event response logic to the common ancestor node of all event targets. At the same time, use the event object .target to get the specific event target node and then get the content of the event target.
Advantages of event delegation
  • Since the event processing logic is bound to the ancestor node instead of each event target, you only need to register the event once in the ancestor node, instead of managing the number of child nodes or dynamically adding child nodes.

Practice process

One: Event capture and event bubbling

The test code is as follows
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>事件流</title>
    <style type="text/css">
        #outer{
    
    width: 150px;height: 150px;background-color: red;}
        #inner{
    
    width: 80px;height: 80px;background-color: green;}
    </style>
</head>
<body>
<div id="outer">outer box
    <div id="inner">inner box</div>
</div>

<script type="text/javascript">
    let inner = document.getElementById('inner');
    let outer = document.getElementById("outer");
    let body = document.getElementsByTagName("body")[0];
    let nodeArr = [inner,outer,body,document,window];
    let tipsArr = ["inner","outer","body","document","window"];
    nodeArr.forEach((ele,index)=>{
    
    
        ele.addEventListener('click',(e)=>{
    
    
            console.log(`${
      
      tipsArr[index]} click 捕获`);
        },true);
	    ele.addEventListener('click',(e)=>{
    
    
	         console.log(`${
      
      tipsArr[index]} click 冒泡`);
	    },false);
    });
</script>
</body>
</html>
Operation result (click the inner area)

Insert picture description here

Small details: the capture and bubbling of target object events are related to the execution order of capture events/bubbling events [tests are as follows]
  • Adjust the order of capture/bubbling event registration
// ...
   nodeArr.forEach((ele, index)=> {
    
    
   // 先为节点注册冒泡事件
        ele.addEventListener('click', (e)=> {
    
    
            console.log(`${
      
      tipsArr[index]} click 冒泡`);
        }, false);
        ele.addEventListener('click', (e)=> {
    
    
            console.log(`${
      
      tipsArr[index]} click 捕获`);
        }, true);
    });
  • Operation result (click the inner area, that is, the target object event is the capture and bubbling of inner)
    Insert picture description here
Capture/bubble interception
  • Event registration code, add event interception (the following example intercepts bubbling)
// ...
    nodeArr.forEach((ele, index)=> {
    
    
        ele.addEventListener('click', (e)=> {
    
    
            console.log(`${
      
      tipsArr[index]} click 捕获`);
        }, true);
        ele.addEventListener('click', (e)=> {
    
    
            console.log(`${
      
      tipsArr[index]} click 冒泡`);
            e.stopPropagation();// 拦截冒泡
        }, false);
    });
  • operation result
    Insert picture description here

Two: event delegation (agent)

The test code is as follows
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>事件委托</title>
    <style>
        ul{
    
    
            float: left;
        }
    </style>
</head>
<body>
<ul>
    <li><button>one</button></li>
    <li><button>two</button></li>
    <li><button>three</button></li>
    <li><button>four</button></li>
    <li><button>five</button></li>
</ul>
<script>
    let ul = document.getElementsByTagName('ul')[0];
    ul.addEventListener('click', (e)=> {
    
    
        let btn = e.target;
        console.log(btn.textContent);
    }, true);// 这里使用捕获、冒泡均可
    function addLi(text){
    
    
        let li = document.createElement('li');
        let btn = document.createElement('button');
        btn.textContent = text;
        li.appendChild(btn);
        ul.appendChild(li);
    }
</script>
</body>
</html>
operation result

Insert picture description here

Guess you like

Origin blog.csdn.net/jw2268136570/article/details/105272809