Event is an object that comes with IE, but this object does not exist in FF. Events can only be simulated by passing parameters.
In addition, srcElement is used in IE to obtain the event trigger source, and target is used in FF. Therefore, relevant browser compatibility needs to be made when using these two objects.
Copy the code code as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>srcElement --//www.VeVB.COm/</title>
<script type="text/javascript">
<!--
function Click(event){
event = event? event: window.event
var obj = event.srcElement? event.srcElement:event.target;
alert(obj.tagName);
}
//-->
</script>
</head>
<body>
<button id="btn" onclick="Click(event)">Click</button>
</body>
</html>
View running results
Comprehensive of attachEvent and addEventListener in the previous section.
Copy the code code as follows:
<script type="text/javascript">
<!
function Click(event){
event = event? event: window.event;
var obj = event.srcElement? event.srcElement:event.target;
alert("eventObj.tabName:" + obj.tagName);
}
var oBtnNew;
window.onload=function(){
oBtnNew=document.getElementById("btnNew");
if(window.attachEvent){
oBtnNew.attachEvent("onclick",hanlder);
oBtnNew.attachEvent("onmouseover",hanlder);
}else{
oBtnNew.addEventListener("click",hanlder,false);
oBtnNew.addEventListener("mouseover",hanlder,false);
}
/* Or use the following test
oBtnNew.onclick=hanlder;
oBtnNew.onmouseover=hanlder;*/
}
function handle(event){
event=event?event:window.event;
if(event.type=="click")
oBtnNew.innerHTML="onclick event occurred";
else if(event.type=="mouseover")
oBtnNew.innerHTML="onmouseover event occurred";
}
//>
</script>
</head>
<body>
<button id="btn" onclick="Click(event)">Click</button>
<button id="btnNew">Add event click</button>