Jquery event trigger Analysis

Source: Internet
Author: User
  1. Registered events, such as onclick. When you click this element, the registered event processing function of the event is automatically triggered. But sometimes we need to useProgramTo trigger an event, you must use force to trigger an event. In IE, we can use. fireevent. For example, <form onsubmit ="()">, If the form is submitted using form. Submit () of the button, the onsumbit event is not triggered automatically. If necessary, $ (": Form") before submit ")[0]. Fireevent ("onsubmit",) to trigger this event.
  2. There are three steps in Mozilla: var EVT = Document. createevent ('Htmlevents');
  3. EVT. initevent ('Change',True,True); T. dispatchevent (EVT );
  4. Prototype is implemented in this way. In jquery, the implementation method is a little different.
  5. Trigger: function (type, Data, FN ){
  6. Return This. Each (function (){
  7. Jquery. event. Trigger (type, data,This,True, FN );
  8. });},
  9. Trigger has three parameters. The data parameter provides real-time transmission for the registered event function. If data [0] Preventdefault exists, data [0. FN is an instant and out-of-the-box event processing method. That is, if an event is not registered, you can pass in a processing function to process the event. If it has already been registered, it will be executed after the original event processing function.
  10. // This method triggers all bound handler functions of the specified event type. However, the default browser action is not executed.
  11. Triggerhandler: function (type, Data, FN ){
  12. Return This[0] & Jquery. event. Trigger (type, data,This[0],False, FN );
  13. },
  14. Triggerhandle sets the donative parameter of jquery. event. TriggerFalseTo prevent the execution of the browser's default processing method. It is a little different from trigger, but it only processes the first element of the jquery object.
  15. The preceding two methods call jquery. event. Trigger to complete the task:
  16. Trigger: function (type, Data, ELEM, donative, extra ){
  17. Data = jquery. makearray (data );// Data can be {XX: yy}
  18. // Getdata is supported! In this form, exclusive = true indicates that
  19. // All functions of the event are executed in different types of namespaces.
  20. If(Type. indexof ("! ")> =0) {①
  21. Type = type. Slice (0,-1); Var exclusive =True;
  22. }
  23. If(! ELEM ){// Process the Global Fire event ②
  24. If(This. Global [type])
  25. Jquery. Each (jquery. cache, function (){
  26. // Find all the elements that register the event from the cache and trigger the processing function of the event.
  27. If(This. Events &&This. Events [type])
  28. Jquery. event. Trigger (type, data,This. Handle. ELEM );
  29. });
  30. }Else{// Process the fire event of a single element event ③
  31. If(ELEM. nodetype =3| ELEM. nodetype =8)ReturnUndefined;
  32. Var val, RET, FN = jquery. isfunction (ELEM [type] |Null),
  33. // If the data parameter is not passed into the browser's event object, the event variable is true.
  34. // If the data parameter itself is a group, true is used when the first element is not the event object of the browser.
  35. // True for event. That is, if no event is passed in, a forged event object is created and data [0] exists.
  36. Event =! Data [0] |! Data [0]. Preventdefault;
  37. // Construct a forged event object without passing in the event object.
  38. If(Event ){// Store the first 4 in the array
  39. Data. unshift ({type: type, target: ELEM,
  40. Preventdefault: function () {}, stoppropagation:
  41. Function () {}, timestamp: Now ()});
  42. Data [0] [Expando] =True;// The forged event object does not need to be corrected.
  43. }
  44. Data [0]. Type = type;// Prevent event name errors
  45. // Perform the classification (namespace) of the event registration function. Not all.
  46. If(Exclusive) data [0]. Exclusive =True;
  47. // Unlike traditional processing methods such as prototype, fireevent is not used.
  48. // Use the event handling method registered to the browser event by fire.
  49. // There are three steps. First, the fire registers the event through jquery. event. Add. This event
  50. // It may be a Custom Event (not registered to a browser event ).
  51. // The second step is the local processing function of the event registered by fire using the ELEM. onclick method.
  52. // Step 3 is the default fire event processing method (registered in the local onclick Mode)
  53. // If it does not exist ).
  54. // Events that are registered using jquery. event. Add are triggered here,
  55. VaR handle = jquery. Data (ELEM,"Handle"); ⑤
  56. If(Handle) val = handle. Apply (ELEM, data );// Here data is divided into multiple parameters
  57. // The processing trigger registers the local processing method through ELEM. onfoo = function,
  58. // But it is not triggered for links's. Click (), this will not be executed through addevent
  59. // Event handling method registered by the method.
  60. If((! FN | (jquery. nodename (ELEM,'A') & Type ="Click") ⑥
  61. & ELEM ["On"+ Type] & ELEM ["On"+ Type]. Apply (ELEM, data) =False)
  62. Val =False;
  63. // The first few additional function parameters are given through data. Here, we will remove the forged event.
  64. // Its last parameter is the result returned by a series of event processing functions, which are generally bool values.
  65. // This function can process a scanning task based on the result.
  66. If(Event) data. Shift ();
  67. // Processing triggers the processing of the function specified by extra.
  68. If(Extra & jquery. isfunction (extra) {7
  69. Ret = extra. Apply (ELEM, val =Null? Data: Data. Concat (VAL ));
  70. // If this function returns a value, the trigger return value is its return value.
  71. // If not, it is the last return value of the series event processing function. Generally bool
  72. If(Ret! = Undefined) val = ret;
  73. }
  74. // Trigger the default local event method, which is in the absence of event registration such as. onclick
  75. // When the previous execution event processing function return value is not false, it will be executed.
  76. // It can also control whether or not to execute through the donative command.
  77. // For example, this. Submit () can be used in form to submit form.
  78. If(FN & donative! =False& Val! =FalseBytes
  79. &&! (Jquery. nodename (ELEM,'A') & Type ="Click")){
  80. This. Triggered =True;
  81. Try{ELEM [type] ();// For some hidden elements, ie reports an error
  82. }Catch(E ){}
  83. }
  84. This. Triggered =False;
  85. }
  86. ReturnVal;
  87. },
  88. The fire event Method of jquery is totally different from the implementation in prototype. EXT and Yui do not provide a method to force the event to be triggered. For general thinking, the fireevent or dispatchevent method should be used to trigger browser events.
  89. But jquery uses a different method. Events registered through jquery. event. Add (whether custom or registered to browser events) are stored in a cache corresponding to the element and event name. In the browser trigger, this is useless. However, it is used to obtain the corresponding event processing function from the cache when the program is forced to trigger. In this case, the browser event is discarded. You can also execute some custom event functions here. For example, place ⑤.
  90. For event functions registered in the form of click or ELEM. onclick = function () {} in HTML tags. In section 6, it can use the callback function in the form of execution elements such as onclick. Only one function can be registered using this dom0 method.
  91. Sometimes, if there is no event processing function such as onclick, the browser will execute the default processing function. For example, form. Submit (). The default event processing can also be controlled by the donative parameter.
  92. The program forces the event to be triggered manually. One problem is how the event is generated, that is, no browser generates the event and passes it into the function. Prototype uses the newly generated dataavailable event. Such events have no effect. Jquery also uses the fake method to forge an event. For example, ④. It is better than prototype events because it can pass in the required event through the parameters of the trigger function. Prototype is not supported.
  93. Through the above analysis, we can see that jquery builds the trigger function by simulating the execution process of the browser trigger event. First, execute the dom1 method (addevent) Registration event, then execute the dom0 method registration event, and finally see whether to execute the default event processing.
  94. In section 7, we can see that trigger may also pass in callback functions and parameters to judge and process the results of the executed event processing function, forming a new result returned through the trigger function. This is useful sometimes.
  95. In addition, it can also classify the event processing functions (namespace), and call the processing functions of different types of events (through jquery. event. add ). The processing of this classification is implemented in handle.
  96. Handle: function (event ){
  97. // Return undefined or false
  98. Var val, RET, namespace, all, handlers;
  99. // Modified the input parameter, which is referenced here.
  100. Event = arguments [0] = Jquery. event. Fix (event | window. Event );
  101. // Namespace Processing
  102. Namespace = event. type. Split (".");
  103. Event. type = namespace [0];
  104. Namespace = namespace [1];
  105. // All = true indicates that any handler and namespace do not exist.
  106. // When event. Exclusive does not exist or is false, all = true.
  107. All =! Namespace &&! Event. Exclusive;
  108. // Find the list of handler functions for cached event names in element events
  109. Handlers = (jquery. Data (This,"Events") | {}) [Event. Type];
  110. For(Var j in handlers ){// Execute each processing function
  111. VaR handler = handlers [J];
  112. // Filter the functions by class
  113. If(ALL | handler. type = namespace ){
  114. // Input references and delete them later
  115. Event. Handler = handler;
  116. Event. Data = handler. Data;// Added when adding
  117. Ret = handler. Apply (This, Arguments );// Execute the event processing function
  118. If(Val! =False)
  119. Val = ret;// If a processing function returns false, this function returns false.
  120. If(Ret =False){// Do not perform the default action of the browser
  121. Event. preventdefault ();
  122. Event. stoppropagation ();
  123. }
  124. }
  125. }
  126. ReturnVal ;},
  127. The main function of handle is to classify and orderly execute all registered processing functions of events.

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.