Source Analysis encapsulated by jquery event

Source: Internet
Author: User
  1. For JavaScript event extensions, all lib operations are similar. With jquery, prototype, Yui, and ext, the primary problem to be solved is compatibility. All lib packages the event and unifies its attributes to solve its compatibility. Operations on events are nothing more than three event Methods: addevent, fireevent, and removeevent. Generally, lib extends the functions provided by the browser to solve problems such as compatibility Memory leakage. The third problem is how to get the domready status.
  2. 6.1Event package
  3. Browser event compatibility is a headache. The IE event is in the global window, while the Mozilla event is passed in to the callback function as the event Source parameter. There are also many event handling methods.
  4. Jquery provides an event package, which is a little simpler than other lib but is sufficient for use.
  5. // Wrap the event.
  6. Fix: function (event ){
  7. If(Event [expando] =True)ReturnEvent;// Indicates that the event has been wrapped
  8. // Save the original event and clone one at the same time.
  9. VaR originalevent = event; ①
  10. Event = {originalevent: originalevent };
  11. For(VAR I =This. Props. length, prop; I ;){
  12. Prop =This. Props [-- I];
  13. Event [prop] = originalevent [prop];
  14. }
  15. Event [expando] =True;
  16. // With preventdefault and stoppropagation, the clone will not run.
  17. Event. preventdefault = function () {②
  18. // Run on the original event
  19. If(Originalevent. preventdefault)
  20. Originalevent. preventdefault ();
  21. Originalevent. returnvalue =False;
  22. };
  23. Event. stoppropagation = function (){
  24. // Run on the original event
  25. If(Originalevent. stoppropagation)
  26. Originalevent. stoppropagation ();
  27. Originalevent. cancelbubble =True;
  28. };
  29. // Corrected Timestamp
  30. Event. timestamp = event. timestamp | now ();
  31. // Modify target
  32. If(! Event.tar get) ③
  33. Event.tar get = event. srcelement | document;
  34. If(Event.tar get. nodetype =3)// The text node is the parent node.
  35. Event.tar get = event.tar get. parentnode;
  36. // Relatedtarget
  37. If(! Event. relatedtarget & event. fromelement) ④
  38. Event. relatedtarget = event. fromelement = event.tar get
  39. ? Event. toelement: event. fromelement;
  40. // Calculate pagex/y if missing and clientx/y available
  41. If(Event. pagex =Null& Event. clientx! =Null) {6
  42. VaR Doc = document.doc umentelement, body = Document. Body;
  43. Event. pagex = event. clientx
  44. + (Doc & Doc. scrollleft | Body & Body. scrollleft |0)
  45. -(Doc. clientleft |0);
  46. Event. Pagey = event. clienty
  47. + (Doc & Doc. scrolltop | Body & Body. scrolltop |0)
  48. -(Doc. clienttop |0);
  49. }
  50. // Add which for key events
  51. If(! Event. Which & (event. charcode | event. charcode =0) 7.
  52. ? Event. charcode: event. keycode ))
  53. Event. Which = event. charcode | event. keycode;
  54. // Add metakey to non-Mac browsers
  55. If(! Event. metakey & event. ctrlkey) Success
  56. Event. metakey = event. ctrlkey;
  57. // Add which for CLICK: 1 = left; 2 = middle; 3 = right
  58. // Note: button is not normalized, so don't use it
  59. If(! Event. Which & event. Button) Success
  60. Event. Which = (event. Button &1?1: (Event. Button &2
  61. ?3: (Event. Button &4?2:0)));
  62. ReturnEvent;
  63. },
  64. The aboveCode① Retain the reference of the original event and clone the original event. Package on this clone event. ② Run the preventdefault and stoppropagation methods on the original event to determine whether to prevent the default event action from occurring and whether to stop the bubbling event and pass it up.
  65. ③ The target is corrected, and srcelement is used in IE. For text node events, the target should be uploaded to its parent node.
  66. ④ Relatedtarget is only useful for mouseout and Mouseover. In ie, the target variables "to" and "from" are divided, which are not separated in Mozilla. To ensure compatibility, use relatedtarget.
  67. 6 is the Coordinate Position of the event. This is relative to page. If the page can be scroll, add scroll to its client. In ie, the border of the default 2px body should be subtracted.
  68. 7. The keyboard event buttons are unified to the event. Which attribute. Ev. charcode in ext | eV. keycode |0Cursor is to unify the mouse event buttons on event. Which. Charcode and Ev. keycode are character keys and not character keys. The & method is used in the processing of the compatibility. EXT solves compatibility issues through the following three lines.
  69. VaR btnmap = ext. isie? {1:0,4:1,2:2}: (Ext. issafari? {1:0,2:1,3:2}:{0:0,1:1,2:2});This. Button = E. Button? Btnmap [E. Button]: (E. Which? E. which-1:-1);
  70. ① (3) (4) (6) (7)

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.