Dom2-level event objects, adding events, blocking default events, blocking bubble events, and obtaining compatible processing of event object targets

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.