Ie and ff under the use of events

  • 2020-03-30 00:00:31
  • OfStack

Event is an object that comes with ie, but it does not exist in ff, so it can only be simulated by passing parameters

In addition, srcElement is used in ie to obtain the event trigger source, and target is used under ff. Therefore, browser compatibility is required when using these two objects.
 
<!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.jb51.net/</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 on the </button> 
</body> 
</html> 

View the results
Combine the attachEvent and addEventListener of the previous section.
 
<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); 
} 
 
} 
function hanlder(event){ 
event=event?event:window.event; 
if(event.type=="click") 
oBtnNew.innerHTML=" happened onclick The event "; 
else if(event.type=="mouseover") 
oBtnNew.innerHTML=" happened onmouseover The event "; 
} 
// � > 
</script> 
</head> 
<body> 
<button id="btn" onclick="Click(event)"> Click on the </button> 
<button id="btnNew"> Add event click </button> 

Related articles: