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;
},