阻止冒泡,阻止默认行为

<!DOCTYPE html >
< html >
     < head >
         < meta charset= "UTF-8" >
         < title ></ title >
         < style >
             .box{
                
                 width: 400px;
                 height: 400px;
                 background: red;
            }
             .box1{
                
                 width: 200px;
                 height: 200px;
                 background: blue;
            }
             .box2{
                
                 width: 100px;
                 height: 100px;
                 background: orange;
            }
         < / style >
         < script >
            
             //事件的target属性 : 存放触发事件的节点对象
             onload = function() {
                
                
                 //事件的冒泡 (浏览器默认的)
                     //事件传递机制: 在页面上是从上层往下层传递事件
                
                 //事件的捕获
                     //事件传递机制: 在页面上是从下层往上层传递事件
                
                 function $( id){
                     return document. getElementById( id);
                }
                 var box= $( 'box');
                 var box1= $( 'box1');
                 var box2= $( 'box2');             
                
                 box. onclick= function(){
                    
                     alert( 'box');
                }               
                 box1. onclick= function(){
                    
                     alert( 'box1');
                }
                
                 box2. onclick= function( e){
                    
                     alert( 'box2');
                    
                     var event= e || window. event;
                     //阻止事件的冒泡 兼容性写法                
                     if( event. stopPropagation){
                         event. stopPropagation()
                    } else{
                         event. cancelBubble= true;
                    }       
                }
                
                
            }
         < / script >
     </ head >
     < body >
         < div class= "box" id= "box" >
            box         
             < div id= "box1" class= "box1" >
                
                box1
                 < div id= "box2" class= "box2" >
                    
                    box2
                 </ div >
            
             </ div >
         </ div >
        
        
        
     </ body >
</ html >


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值