这是一种浏览器兼容手段。
onclick事件函数如果是通过javascript设置的,那么IE6-8浏览器不会通过函数参数传入event事件对象,直接读取event参数的话会是undefined。但firefox浏览器是可以直接读取的。
IE6-8必须通过window.event读取。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<script>
function test(event) {
/*if (!event) {
event = window.event;
}*/
alert(event);
}
</script>
</head>
<body onload="document.getElementById('btn').onclick = test">
<input type="button" id="btn" value="按钮">
</body>
</html>