jquery Source Analysis---event analysis

Source: Internet
Author: User

Package of 6.1 Event

Browser event compatibility is a troubling issue. IE's event is under the Global window, and Mozilla's event is the incident source parameter passed into the callback function. There are also many ways to handle events.

jquery provides a package of event, which is a little simpler than the rest of Lib, but enough to use.

Parcel out the event.
Fix:function (event) {
if (event [expando] = = True) return event;//indicates that the event has been wrapped
Save the original event and clone one at the same time.
var originalevent = event; ①
event = {Originalevent:originalevent};
for (var i = this.props.length, prop;i;) {
Prop = This.props[--i];
Event[prop] = Originalevent[prop];
}
Event[expando] = true;
Plus preventdefault and stoppropagation, not running in clone
Event.preventdefault = function () {②
Run on the original event
if (Originalevent.preventdefault)
Originalevent.preventdefault ();
Originalevent.returnvalue = false;
};
Event.stoppropagation = function () {
Run on the original event
if (originalevent.stoppropagation)
Originalevent.stoppropagation ();
Originalevent.cancelbubble = true;
};
Fixed TimeStamp
Event.timestamp = Event.timestamp | | Now ();
Fixed target
if (!event.target) ③
Event.target = Event.srcelement | | Document
if (Event.target.nodeType = = 3)//Text node is the parent node.
Event.target = Event.target.parentNode;
Relatedtarget
if (!event.relatedtarget && event.fromelement) ④
Event.relatedtarget = Event.fromelement = = Event.target
? Event.toElement:event.fromElement;
Calculate pagex/y if missing and clientx/y available
if (Event.pagex = = null && event.clientx!= null) {⑥
var doc = document.documentelement, BODY = document.body;
Event.pagex = Event.clientx
+ (Doc && Doc.scrollleft | | body && Body.scrollleft | | 0)
-(Doc.clientleft | | 0);
Event.pagey = Event.clienty
+ (Doc && Doc.scrolltop | | body && Body.scrolltop | | 0)
-(Doc.clienttop | | 0);
}

Add which for key events
if (!event.which && (Event.charcode | | event.charcode = = 0) ⑦
? Event.charCode:event.keyCode))
Event.which = Event.charcode | | Event.keycode;

Add Metakey to Non-mac browsers
if (!event.metakey && event.ctrlkey) ⑧
Event.metakey = Event.ctrlkey;
Add which for click:1 = = left; 2 = middle; 3 = Right
Note:button isn't normalized, so don ' t use it
if (! Event.which && Event.button) ⑨
Event.which = (Event.button & 1 1: (Event.button & 2
? 3: (Event.button & 4? 2:0));
return event;
},

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.