【js】基础练习(1), 取消事件冒泡

html部分

<input type="button" id="btn1" value="按钮" />
<div id="div1"></div>

css部分

<style type="text/css">
#div1{
width: 100px;
height:100px ;
background:#ccc ;
display:none ;
}
</style>

js部分

<script type="text/javascript">
window.onload=function(){
obtn1=document.getElementById('btn1');
odiv1=document.getElementById('div1');
obtn1.onclick=function(ev){
var oEvent=ev||event; //兼容IE跟非IE

oEvent.cancelBubble=true; //取消事件冒泡
odiv1.style.display='block';
}
document.onclick=function(){
odiv1.style.display='none';
}
}
</script>

posted @ 2017-06-19 10:05  阳光透过幸福  阅读(292)  评论(0编辑  收藏  举报