代码
<html>
<head>
<title></title>
</head>
<body>
<div id="contentq" onclick="bb(event)"><!--显示传入event对象-->
fasdf
</div>
</body>
<script type="text/javascript">
function bb(e){
alert(e);
}
var container = document.getElementById("contentq");
container.onclick=function(e){
if(document.all){//IE
var a=window.event;
alert(a.type);
}else{
alert(e.type);
}
}
</script>
</html>
<head>
<title></title>
</head>
<body>
<div id="contentq" onclick="bb(event)"><!--显示传入event对象-->
fasdf
</div>
</body>
<script type="text/javascript">
function bb(e){
alert(e);
}
var container = document.getElementById("contentq");
container.onclick=function(e){
if(document.all){//IE
var a=window.event;
alert(a.type);
}else{
alert(e.type);
}
}
</script>
</html>
事件对象获取统一的方法:(来源于网络)
代码
function getEvent(event) {
var ev = event || window.event;
if (!ev) {
var c = this.getEvent.caller;
while (c) {
ev = c.arguments[0];
if (ev && (Event == ev.constructor || MouseEvent == ev.constructor)) { /怿飞注:YUI 源码 BUG,ev.constructor 也可能是 MouseEvent,不一定是 Event
break;
}
c = c.caller;
}
}
return ev;
}
var ev = event || window.event;
if (!ev) {
var c = this.getEvent.caller;
while (c) {
ev = c.arguments[0];
if (ev && (Event == ev.constructor || MouseEvent == ev.constructor)) { /怿飞注:YUI 源码 BUG,ev.constructor 也可能是 MouseEvent,不一定是 Event
break;
}
c = c.caller;
}
}
return ev;
}
调用执行的是:
function foo(){
var e = getEvent();
alert(e);
}
复习下事件,长年用JQ,都给忘了~