Recommended event capture function addevent ()

Source: Internet
Author: User

1. the following method is recommended by the father of jquery to add and remove events.

Java code
  1. /*

     
  2. * OBJ: the object to bind the event

     
  3. * Type indicates the event type, such as "click", "LOAD", "Submit", and "Mouseover ".

     
  4. * FN function name

     
  5. */


  6. Function addevent (OBJ, type, FN)
  7. {

  8. If


    (Obj. attachevent)
  9. {
  10. OBJ [
    'E'

    + Type + FN] = FN;
  11. OBJ [type + FN] = function ()
  12. {
  13. OBJ [
    'E'

    + Type + FN] (window. Event );
  14. }
  15. OBJ. attachevent (
    'On'

    + Type, OBJ [type + FN]);
  16. }

  17. Else


  18. OBJ. addeventlistener (type, FN,
    False


    );
  19. }
  20. /*

     
  21. * OBJ: the object of the event to be deleted.

     
  22. * Type indicates the event type, such as "click", "LOAD", "Submit", and "Mouseover ".

     
  23. * FN function name

     
  24. */


  25. Function removeevent (OBJ, type, FN)
  26. {

  27. If


    (Obj. detachevent)
  28. {
  29. OBJ. detachevent (
    'On'

    + Type, OBJ [type + FN]);
  30. OBJ [type + FN] =
    Null


    ;
  31. }

  32. Else


  33. OBJ. removeeventlistener (type, FN,
    False


    );
  34. }
  35. // Addevent (document. getelementbyid ('foo'), 'click', dosomething );


2. according to "Pro JavaScript techniques", John resig greatly recommends the addevent () method of Dean Edwards, but does not recommend more streamlined works. He commented on Edwards's addevent:

1

It can work in all browsers, even if it is ancient and does not support any browsers;
  1. 2

    ,
    This


    Keywords can be used in all binding functions, pointing to the current element;
  2. 3

    And all browser-specific functions that prevent default browser behavior and prevent event bubbles;
  3. 4

    Regardless of the browser type, the event object is always passed in as the first object;
  4. 5

    The only drawback is that it only works in the bubble stage (because the traditional method of event binding is used in depth ).
  5. 6

    , The addevent/removeevent function is so powerful that there is no reason not to use it in your code.
1. It can work in all browsers, even if it is old and does not support any browsers; 2. This keyword can be used in all binding functions, pointing to the current element; 3, all browser-specific functions that prevent browser default behaviors and prevent event bubbles; 4. Regardless of the browser type, the event object is always passed in as the first object; 5, the only drawback is that it only works in the bubble stage (because the traditional method of event binding is used in depth ). 6. The addevent/removeevent function is so powerful that there is no reason not to use it in your code.

Dean Edwards said:
My solution is very different.

* It performs no object detection
* It does not use the addeventlistener/attachevent Methods
* It keeps the correct scope (the this keyword)
* It passes the event object correctly
* It is entirely cross-browser (it will probably work on ie4 and ns4)
* And from what I can tell it does not leak memory

The following is the addevent/removeevent function of Dean Edwards:

Java code
  1. // Http://dean.edwards.name/weblog/2005/10/add-event/


  2. Function addevent (element, type, Handler ){

  3. If


    (Element. addeventlistener ){
  4. Element. addeventlistener (type, handler,
    False


    );
  5. }
    Else


    {

  6. // Assign an independent ID to each event processing function


  7. If


    (! Handler. $ guid) handler. $ guid = addevent. guid ++;

  8. // Create an event-type hash table for the element


  9. If


    (! Element. Events) element. Events = {};

  10. // Create an event handler hash table for each element/event

  11. VaR handlers = element. events [type];

  12. If


    (! Handlers ){
  13. Handlers = element. events [type] = {};

  14. // Store existing event processing functions (if one already exists)


  15. If


    (Element [
    "On"

    + Type]) {
  16. Handlers [
    0

    ] = Element [
    "On"

    + Type];
  17. }
  18. }

  19. // Store the event handler in the hash table

  20. Handlers [handler. $ guid] = handler;

  21. // Assign a global event processing function to handle all tasks

  22. Element [
    "On"

    + Type] = handleevent;
  23. }
  24. };
  25. // Create a counter with an independent ID

  26. Addevent. guid =
    1

    ;
  27. Function removeevent (element, type, Handler ){

  28. If


    (Element. removeeventlistener ){
  29. Element. removeeventlistener (type, handler,
    False


    );
  30. }
    Else


    {

  31. // Delete the event handler from the hash table


  32. If


    (Element. Events & element. events [type]) {
  33. Delete element. events [type] [handler. $ guid];
  34. }
  35. }
  36. };
  37. Function handleevent (event ){
  38. VaR returnvalue =
    True


    ;

  39. // Obtain the event processing object (ie uses the global event object)

  40. Event = event | fixevent (((
    This


    . Ownerdocument |
    This


    . Document |
    This


    ). Parentwindow | window). Event );

  41. // Obtain the reference of the hash table of the event processing function

  42. VaR handlers =
    This


    . Events [event. Type];

  43. // Process each event processing function in sequence


  44. For


    (Var I in handlers ){

  45. This


    . $ Handleevent = handlers [I];

  46. If


    (
    This


    . $ Handleevent (event) ===
    False


    ){
  47. Returnvalue =
    False


    ;
  48. }
  49. }

  50. Return


    Returnvalue;
  51. };
  52. // Add some methods for the lack of IE event objects

  53. Function fixevent (event ){

  54. // Added W3C standard event Methods

  55. Event. preventdefault = fixevent. preventdefault;
  56. Event. stoppropagation = fixevent. stoppropagation;

  57. Return


    Event;
  58. };
  59. Fixevent. preventdefault = function (){

  60. This


    . Returnvalue =
    False


    ;
  61. };
  62. Fixevent. stoppropagation = function (){

  63. This


    . Cancelbubble =
    True


    ;
  64. };

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.