阻止事件冒泡和默认行为

<script type="text/javascript">
 window.onload=function(){
 var a=document.getElementById("a");
 var b=document.getElementById("b");
 var c=document.getElementById("c");
 var c=document.getElementById("d");

     a.onclick=function(e){
         this.style.background="#000";    
         };

     b.onclick=function(e){
             this.style.background="#ccc";
             //阻止事件冒泡
             window.event.cancelBubble = true;//IE8以下
             e.stopPropagation();
         };

     d.onmousedown=function(e){
         //阻止默认事件,比如在chrome下图片有拖拽默认行为
         window.event.returnValue = false;
         e.preventDefault();
     }
 }

 

 </script>
参考于: http://www.jb51.net/article/36818.htm
 
<div id="a" style="width:300px;height:300px;background:red;overflow:hidden;">
     <div id="b" style="width:200px;height:200px;background:green;margin:50px 0 0 50px;overflow:hidden;">
         <div id="c" style="width:100px;height:100px;background:yellow;margin:50px 0 0 50px;overflow:hidden;">
             <img src="240x240.jpg" width="50" height="50" id="d" />
         </div>
     </div>
 </div>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值