*/
事件流:事件的传递方式
冒泡:
事件由最具体的元素开始,层层向外传递,直到最外层元素
主流浏览器:冒泡机制
捕获:
由最外层元素开始,层层向内传递,直到具体元素
阻止事件冒泡:
e.stopPropagation();
IE:
e.cancelBubble=true;
*/
//css样式代码
#box1{
width: 200px;
height: 200px;
background-color: red;
}
#box2{
width: 150px;
height: 150px;
background-color: blue;
}
#box3{
width: 100px;
height: 100px;
background-color: green;
}
//html代码
<div id="box1">
<div id="box2">
<div id="box3"></div>
</div>
</div>
//JS代码
var o1=document.getElementById('box1');
var o2=document.getElementById('box2');
var o3=document.getElementById('box3');
//分别添加点击事件
o1.function(e){
alert('红色');
//var event=window.event;//IE
//e.stopPropagation();//阻止事件冒泡
// event.stopPropagation();
// event.cancelBubble=true;
/*if(e==undefined){
e=window.event;//IE
e.cancelBubble=true;
}else{
e.stopPropagation();//其他浏览器
}*/
//阻止冒泡
e?e.stopPropagation():window.event.cancelBubble=true;//兼容性写法
};
o2.function(e){
alert('蓝色');
e.stopPropagation();
};
o3.function(){
alert('绿色');
};
事件流
最新推荐文章于 2022-04-14 14:37:31 发布