<!--cross-browser event compatibility--><! DOCTYPE html>"Utf-8"> <body> <div> <a href="http://www.baidu.com"Id="Al">click me</a> </div> </body><script type="Text/javascript">varEventutil={addhindler:function (ele,type,func) {//Add Event if(Ele.addeventlistener) {//In addition to IE8 and out of browser compatibleEle.addeventlistener (Type,func,false); } Else if(ele.attachevent) {//IE8 and the following versions of the browserEle.attachevent (" on"+Type,func); } Else{ele[" on"+type]=func}//compatible with all versions}, Removehindler:function (Ele,type,func) {//To Delete a binding event if(Ele.removeeventlistener) {//In addition to IE8 and out of browser compatibleEle.removeeventlistener (Type,func,false); } Else if(ele.dettachevent) {//IE8 and the following browsersele.dettachevent[" on"+type]=func; } Else{ele[" on"+type]=func;} }, Getevent:function (Event){//Get Events Event return Event?Event: Window.Event;//compatible with IE for event}, Preventdefault:function (Event){//compatible with IE block event default behavior Event. preventdefault?Event. Preventdefault ():Event. returnvalue=false; }, Gettarget:function (Event){//compatible with IE to get event targets return Event. target| |Event. srcelement; }, Stoppropagation:function (Event){//compatible with IE stop event bubbling Event. stoppropagation?Event. Stoppropagation ():Event. cancelbubble=true; }, Getrelatedtarget:function (Event){//MouseOver and mouseout have related element information, other mouse events return null if(Event. Relatedtarget) { return Event. Relatedtarget; } Else if(Event. Fromelement) { return Event. fromelement; } Else if(Event. Toelement) { return Event. toelement; } Else return NULL; }, Getbutton:function (Event){//Get the mouse button information, 0 for left, 1 for the scroll wheel, 2 for the right button if(Document.implementation.hasFeature ("mouseevents","2.0")){ return Event. Button; } Else{ Switch(Event. Button) { Case 0: Case 1: Case 3: Case 5: Case 7: return 0;//return the button with the left arrow (primary key) to the primary key Case 2: Case 6: return 2;//Assign a secondary key with a right key (secondary key) without a primary key Case 4: return 1//turn the rollers into rollers only } } }, } varAl=document.getelementbyid ("Al"); function Fun (Event){ Event=eventutil.getevent (Event); Eventutil.stoppropagation (Event);//Stop event bubblingEventutil.preventdefault (Event);//block default behaviorEventutil.getrelatedtarget (Event); Eventutil.getbutton (Event); } Eventutil.addhindler (Al,"Click", fun)//To add a binding event</script>javascript--event compatibility (partial)