event是ie自带的一个对象,而ff中不存在该对象,只能通过传递参数(并且惟一)的方式来实现event,:(
关于event的详细区别请查看这篇文章 window.event对像在IE与FF中的区别
另外ie中使用srcElement去获得事件触发源,ff下使用target。因此使用这两个对象时需要做相关的浏览器兼容。
1<script type="text/javascript">
2<!–
3function Click(event){
4 event = event? event: window.event
5 var obj = event.srcElement ? event.srcElement:event.target;
6 alert(obj.tagName);
7}
8//–>
9</script>
10</head>
11
12<body>
13<button id="btn" onclick="Click(event)">点击</button>
14</body>
2<!–
3function Click(event){
4 event = event? event: window.event
5 var obj = event.srcElement ? event.srcElement:event.target;
6 alert(obj.tagName);
7}
8//–>
9</script>
10</head>
11
12<body>
13<button id="btn" onclick="Click(event)">点击</button>
14</body>
综合上节attachEvent与addEventListener。
1<script type="text/javascript">
2<!–
3function Click(event){
4 event = event? event: window.event;
5 var obj = event.srcElement ? event.srcElement:event.target;
6 alert("eventObj.tabName:" + obj.tagName);
7}
8var oBtnNew;
9window.onload=function(){
10 oBtnNew=document.getElementById("btnNew");
11 if(window.attachEvent){
12 oBtnNew.attachEvent("onclick",hanlder);
13 oBtnNew.attachEvent("onmouseover",hanlder);
14 }else{
15 oBtnNew.addEventListener("click",hanlder,false);
16 oBtnNew.addEventListener("mouseover",hanlder,false);
17 }
18/*或者采用下面试
19 oBtnNew.onclick=hanlder;
20 oBtnNew.onmouseover=hanlder;*/
21}
22function hanlder(event){
23 event=event?event:window.event;
24 if(event.type=="click")
25 oBtnNew.innerHTML="发生了onclick事件";
26 else if(event.type=="mouseover")
27 oBtnNew.innerHTML="发生了onmouseover事件";
28}
29//–>
30</script>
31</head>
32
33<body>
34<button id="btn" onclick="Click(event)">点击</button>
35<button id="btnNew">添加事件点击</button>
2<!–
3function Click(event){
4 event = event? event: window.event;
5 var obj = event.srcElement ? event.srcElement:event.target;
6 alert("eventObj.tabName:" + obj.tagName);
7}
8var oBtnNew;
9window.onload=function(){
10 oBtnNew=document.getElementById("btnNew");
11 if(window.attachEvent){
12 oBtnNew.attachEvent("onclick",hanlder);
13 oBtnNew.attachEvent("onmouseover",hanlder);
14 }else{
15 oBtnNew.addEventListener("click",hanlder,false);
16 oBtnNew.addEventListener("mouseover",hanlder,false);
17 }
18/*或者采用下面试
19 oBtnNew.onclick=hanlder;
20 oBtnNew.onmouseover=hanlder;*/
21}
22function hanlder(event){
23 event=event?event:window.event;
24 if(event.type=="click")
25 oBtnNew.innerHTML="发生了onclick事件";
26 else if(event.type=="mouseover")
27 oBtnNew.innerHTML="发生了onmouseover事件";
28}
29//–>
30</script>
31</head>
32
33<body>
34<button id="btn" onclick="Click(event)">点击</button>
35<button id="btnNew">添加事件点击</button>