JavaScript事件

JavaScript事件初步认识

<!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>
    <style>
    #box1{
        background-color: aquamarine;
        width: 400px;
        height: 200px;
    }
    </style>
</head>
<body>
    <input type="button" value="test1" onclick='alert("this is test1");'/>
    <input type="button" value="test2" onclick='test()'/>
    <input type="button" value="test3" id="btn1"/>
    <input type="button" value="test4" id="btn2"/>
    <input type="button" value="test5" id="btn3"/>
    <input type="button" value="test6" id="btn4"/>


    <div id="box1">
        <input type="button" value="test7" id="btn5"/>
    </div>
    <script>     
       function test(){
           alert("this is test2");
       }

    //    匿名函数
       var btn1=document.getElementById('btn1');
        btn1.onclick=function(){
            alert("this is test3");
        }
        
        var btn2=document.getElementById('btn2');
        function test1(){
            alert("this is test4");
        }
        btn2.onclick=test1;

        var btn3=document.getElementById('btn3');
        var count=0;
        btn3.onclick=function(){
            alert(count++);
            if(count==3){
                btn3.onclick=null;
            }
        }

        var btn4=document.getElementById('btn4');
        btn4.onclick=function(e){
            var e=e || window.event;
            // 跨浏览器兼容问题
            alert(e);
        }
        // btn4.οnclick=function(e){
        //     alert(e.type);
        // }
         

        // 冒泡排序 
        var btn5=document.getElementById('btn5');
        btn5.onclick=function(){
            alert("this is test6");
            //取消冒泡
            var e=e || window.event;
            //w3c取消冒泡
         //   e.stopPropagation();
            //e.cancelBubble=ture;
         //   alert(e.cancelBubble);
         if(typeof e.cancelBubble=='undefined'){
             e.stopPropagation();
         }else{
             e.cancelBubble=true;
         }
        }
        var box1=document.getElementById('box1');
        box1.onclick=function(){
            alert("this is test of box1");
        }
        document.body.onclick=function(){
            alert("this is test of body");
        }
        document.documentElement.onclick=function(){
            alert("this is test of documentElement");
        }
        document.onclick=function(){
            alert("this is test of document");
        }
 </script>
</body>
</html>

DOM2级事件绑定及移除

DOM2级事件定义了两个方法,用于添加事件和删除事件处理程序的操作:addEventListener()和removeEventListener(),所有DOM节点中都含有这两个方法,并且他们都接受3个参数,事件名、函数、冒泡或不活的布尔值(true表示捕获,false表示冒泡)。
IE事件处理程序,IE实现了DOM中的类似两个方法,attachEvent()和detachEvent()这两个方法接受相关的两个参数,时间处理程序名和事件处理程序函数,在IE8及之前的版本只支持事件冒泡。

绑定

<!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>
    <style>
    #box1{
        background-color: aquamarine;
        width: 400px;
        height: 200px;
    }
    </style>
</head>
<body>
    <div id="box1">
        <input type="button" value="test1" id="btn1"/>
    </div>
    <script>  
        var btn1=document.getElementById("btn1");
        var box1=document.getElementById("box1");
        // btn1.addEventListener('click',function(){
        //     alert('button clicked');

        // },false);   
        btn1.addEventListener('click',function(e){
            alert('button clicked');
            var e=e || window.event;
            if(typeof e.cancelBubble=='undefined'){
                e.stopPropagation();
            }else{
                e.cancelBubble=true;
            }

        },true);   

        box1.addEventListener('click',function(){
            alert("div clicked");
        },false);

        document.body.addEventListener('click',function(){
            alert("body clicked");
        },false);

        document.documentElement.addEventListener('click',function(){
            alert("documentElement click");
        },false);

        document.onclick.addEventListener('click',function(){
            alert("click clicked");
        },false);    
 </script>
</body>
</html>

取消

<!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>
    <style>
    #box1{
        background-color: aquamarine;
        width: 400px;
        height: 200px;
    }
    </style>
</head>
<body>
    <div id="box1">
        <input type="button" value="test1" id="btn1"/>
    </div>
    <script>
    var btn1=document.getElementById("btn1");
    var count=0;
    var hander=function(){
        alert(count++);
        if(count==3){
            alert('事件被取消');
            btn1.removeEventListener('click',hander,false);
        }
            }
    btn1.addEventListener('click',hander,false);  
 </script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

uncle_Huang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值