window.event
属性:
1.srcElement 获取事件源
2.keyCode 获取键盘信息
3.clientX, clientY, 鼠标相对于窗口左上角的坐标
4.screenX,screenY 鼠标相对于屏幕左上角的坐标
5.returnValue 事件返回值
6.cancelBubble 阻止事件传播到包容对象。例如当body和div同时定义了onclick事件时,可组织对div的onclick传播到body(赋值为true)。
实例1
实例2 鼠标拖动实例
属性:
1.srcElement 获取事件源
2.keyCode 获取键盘信息
3.clientX, clientY, 鼠标相对于窗口左上角的坐标
4.screenX,screenY 鼠标相对于屏幕左上角的坐标
5.returnValue 事件返回值
6.cancelBubble 阻止事件传播到包容对象。例如当body和div同时定义了onclick事件时,可组织对div的onclick传播到body(赋值为true)。
实例1
<body onclick="show('body')" oncontextmenu="show1()" onbeforeunload="event.returnValue='你确定离开?'">
<div onclick="show('div')">aaa</div>
<script>
function show(str){
alert(str);
if(str=='div')
event.cancelBubble=true; //阻止继续弹出'body'
}
function show1(){
alert("禁止使用右键!");
event.returnValue=false;
}
</script>
</body>
实例2 鼠标拖动实例
<html>
<head>
<style>
#one{
width:120px;
height:20px;
position:absolute;
background:#666;
text-align:center;
color:white;
}
</style>
</head>
<body>
<div id="one" style="left:10px;top:10px">drag me</div>
<script>
var one = document.getElementById('one');
var pressed = false;
var cx =0;
var cy =0;
document.onmousemove=function(e){
//firefox的event对象要通过函数中的参数获取,而IE可以在window对象中获取
if(pressed){
var ev=e || window.event;
one.style.left=ev.clientX-cx;
one.style.top=ev.clientY-cy;
}
}
one.onmousedown=function(e){
var ev=e || window.event;
if(!pressed){
pressed=true
cx=ev.clientX-parseInt(one.style.left);
cy=ev.clientY-parseInt(one.style.top);
}
}
one.onmouseup=function(){
if(pressed)
pressed=false;
}
</script>
</body>
</html>