ES5-03 Event Monitoring-Delegation

Event binding

  1. Bind on the dom element, not recommended
  2. Binding in js code (multiple bindings only execute the last one)
  3. Binding event listener (multiple listeners are executed one by one)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <input type="button" id="testDom" value="button">
    <script>        
        //事件绑定
        // 1. 在dom元素上绑定 ,不推荐
        // 2. js代码中绑定
        // 3. 绑定事件监听
        testDom.onclick = function(){
     
     
            console.log('ppp');
        }
        testDom.onclick = function(){
     
     
            console.log('11');
        }

        var fun1 = function(){
     
     
            console.log('aaaa');
        }
        var fun2 = function(){
     
     
            console.log('bbb');
        }

        //事件监听
        testDom.addEventListener('click',fun1)

        testDom.addEventListener('click',fun2)
    
        testDom.removeEventListener('click',fun1);

        //ie6-ie8
        testDom.attchEvent('onclick',function(){
     
     

        })      
    </script>
</body>
</html>

Guess you like

Origin blog.csdn.net/weixin_42549581/article/details/103237044