Copy Code code as follows:
var eventutil={//cross-Browser Handler---creation method
Addhandler:function (Element,type,handler) {
if (Element.addeventlistener) {
Element.addeventlistneter (Type,handler,false);
}else if (element.attachevent) {
Element.attachevent ("on" +type,handler);
}else{
element["on" +type]=handler;
}
}
Removehandler:function (Element,type,handler) {//cross-Browser Handler---Delete method
if (Element.removeeventlistener) {
Element.removeeventlistneter (Type,handler,false);
}else if (element.detachevent) {
Element.detachevent ("on" +type,handler);
}else{
element["on" +type]=handler;
}
}
Getevent:function (Event) {//cross-Browser Events object---Returns a reference to an event object
return event?event:window.event;
}
Gettarget:function (Event) {//cross-Browser Events object---return event target
return event.target| | Event.srcelement;
}
Preventdefault:function (Event) {//cross-Browser Events object---Cancel default event
if (Event.preventdefault) {
Event.preventdefault ();
}else{
Event.returnvalue=false;
}
}
Stoppropagation:function (Event) {//cross-browser Event object---block flow of events
if (event.stoppropagation) {
Event.stoppropagation ();
}else{
Event.cancebubble=false;
}
}
Getrelatedtarget:function (Event) {//cross-browser to get related elements
if (event.relatedtarget) {
return event.relatedtarget;
}else if (event.toelement) {
return envent.toelement;
}else if (event.fromelement) {
return event.fromelement;
}else{
return null;
}
}
Getbutton:function (Event) {//Mouse event button Property detection
if (Document.implementation.hasFeature ("MouseEvent", "2.0")) {
return Event.button;
}else{
Switch (Event.button) {
Case 0:
Case 1:
Case 3:
Case 5:
Case 7:
return 0;
Case 2:
Case 6:
Return 2
Case 4:
Return 1
}
}
}
Getcharcode:function (Event) {//cross-browser character encoding---charcode property detection
if (typeof event,charcode== "number") {
return event.charcode;
}else{
return event.keycode;
}
}
}
Event delegates: Too many solutions for event handlers that reduce memory and improve performance;
Simulation event: This is more complex, to be studied slowly;