您的位置:首页 > 其它

event对象和事件冒泡应用

2013-12-10 00:13 232 查看
什么是event对象?

  用来获取事件的相信信息。

例:(实现鼠标点击其他位置隐藏弹出框)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Document</title>
<style>
*{
margin: 0;
padding: ;
}
body{
padding: 100px;
}
#div1{
height: 300px;
width: 100px;
background-color: #ccc;
display: none;
}
</style>
<script>
window.onload=function(){
var oBtn = document.getElementById('btn');
var oDiv = document.getElementById('div1');

oBtn.onclick=function(ev){
var oEvent = ev||event;
oDiv.style.display='block';
//时间对象阻止冒泡
oEvent.cancelBubble=true;
}

document.onclick=function(){
oDiv.style.display='none';
}
}
</script>
</head>
<body>
<input type="button" value="显示" id="btn" />
<div id="div1"></div>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: