ES5-03 事件监听 - 委托

事件绑定

  1. 在dom元素上绑定 ,不推荐
  2. js代码中绑定(多个绑定只执行最后一个)
  3. 绑定事件监听 (多个监听逐条执行)
<!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>

猜你喜欢

转载自blog.csdn.net/weixin_42549581/article/details/103237044