我的问题是:
1.oEvent在这段代码中起了什么作用?
oEvent function handle(oEvent) 的传入参数,从本文看则是一个事件对象;
2.oImg.onclick = handle;和oImg.onmouseover = handle;这两条语句是不是将oImg.onclick和oImg.onmouseover作为参数传到了函数handle里面?
不是,是把 function handle(oEvent) 做为 oImg.onclick,oImg.onmouseove 的响应代码。也就是当onclick事件时,会执行handle程序。
3.为什么这里的handle()要带参数oEvent?
否则 handle() 也不知道是哪个对象触发的事件,
3.if(window.event) oEvent = window.event;如果这个if成立的话,oEvent的值变成了什么?
这句的标准写法如下,
if(window.event)
oEvent = window.event;
1.oEvent在这段代码中起了什么作用?
oEvent function handle(oEvent) 的传入参数,从本文看则是一个事件对象;
2.oImg.onclick = handle;和oImg.onmouseover = handle;这两条语句是不是将oImg.onclick和oImg.onmouseover作为参数传到了函数handle里面?
不是,是把 function handle(oEvent) 做为 oImg.onclick,oImg.onmouseove 的响应代码。也就是当onclick事件时,会执行handle程序。
3.为什么这里的handle()要带参数oEvent?
否则 handle() 也不知道是哪个对象触发的事件,
3.if(window.event) oEvent = window.event;如果这个if成立的话,oEvent的值变成了什么?
这句的标准写法如下,
if(window.event)
oEvent = window.event;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>事件的类型</title>
<script language="javascript">
function handle(oEvent){//这时oEvent不存在,因为oImg.onclick或oImg.onmouseover没有传递参数
var oDiv = document.getElementById("display");
if(window.event) oEvent = window.event;//这时oEvent=事件(click或mouseover)的对象
if(oEvent.type == "click")//事件的类型(click)
oDiv.innerHTML += "你点击了我  ";
else if( oEvent.type == "mouseover")//事件的类型(mouseover)
oDiv.innerHTML += "你移动到我上方了  ";
}
window.onload = function(){
var oImg = document.getElementsByTagName("img")[0];
oImg.onclick = handle;//给oImg设定onclick事件的处理函数,相当于oImg.onclick = function(){handle()}
oImg.onmouseover = handle;//给oImg设定onmouseover事件的处理函数,相当于oImg.onmouseover = function(){handle()}
}
</script>
</head><body>
<img src="http://www.csdn.net/csdnhomepage/images/csdnindex_piclogo.gif" /></body>
<div id="display"></div>
</body>
</html>