取得event对象的变通方法
IE和firefox的事件模型有一定差别,firefox中必须手动传一个event对象给函数,函数中才可以使用
而我们一般有时候是这样写事件
<input type="button" onclick="fire()"/>
这个时候在IE中直接在fire函数中用window.event即可,firefox中就无能为力了,如果改成fire(e)
在fire函数中用e = window.event || e;发现还是不对。
原来系统默认套了一层函数使之成为onclick=function MouseClick(event){fire()};
那么如何在fire中取得event对象呢?
看下面代码,可能有BUG,欢迎大家讨论。
-------------------
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
<script language="javascript">
function getEvent()
{
if( window.event ) return window.event;
var f = arguments.callee;
while(caller= f.caller){f=f.caller;}
return f.arguments[0];
}
function test(){
e = getEvent();
alert(e.clientX);
}
function funb(){
e = getEvent();
alert(e.clientX);
}
</script>
</head>
<body>
<input type=button value="test" onclick="test()">
<script language="javascript">
document.getElementsByTagName("input")[0].onmouseover=function(){
funb();
}
</script>
</body>
</html>
<script>
</script>
---------------
这样我们就可以像使用window.event一样方便的使用事件对象了。