Event object-compatible Processing
1/* 2 * function: event object compatibility 3 * parameter: indicates the event object E4 */5 function getevent (e) {6 7 // If e exists, return directly; otherwise, return window. event8 return E | window. event; 9}
Get the target of the event-compatible Processing
1/* 2 3 * function: Get the target 4 5 * parameter corresponding to the event: the event object E 6 7 */8 9 function gettargetbyevent () of the regular Browser () {10 11 // If e.tar get exists, return directly; otherwise, return window. event. srcelement12 13 return e.tar GET | window. event. srcelement; 14 15}
Add event-compatible Processing
1/* 2 3 * function: Add event 4 5 * parameter: 6 7 */8 9 function addeventhandler (element, eventname, Handler) {10 11 // three (condition) operator. If document. addeventlistener is called directly; otherwise, document is called. attachevent this method 12 13 document. addeventlistener? Element. addeventlistener (eventname, handler, flase): element. attachevent ('on' + eventname, Handler); 14 15}
Block default events-compatible Processing
1/* 2 3 * function: block default event 4 5 * parameter: indicates the event object to be blocked 6 7 */8 9 function stopdefaultevent (E) {10 11 // The Third-Order (condition) operator. If e. preventdefault is called directly; otherwise, window is used. event. returnvalue = false; 12 13 E. preventdefault? E. preventdefault (): (window. event. returnvalue = false); 14 15}
Blocking bubble events-compatible Processing
1/* 2 3 * function: block a bubble event 4 5 * parameter: indicates the event object to be blocked 6 7 */8 9 function stopbubbleevent (e) {10 11 E. stoppropagation? E. stoppropagation (): (window. event. cancelbubble = true); 12 13}
These are compatible processing for some event objects.
In fact, JavaScript compatibility processing is relatively simple. It is nothing more than determining whether the browser has this method (object). If so, directly call (obtain); otherwise, use another method.