Read jquery Eight wrapper event object _jquery

Source: Internet
Author: User
For example, stop event bubbling ie with cancelbubble, standard browser with stoppropagation.
Get event source object, ie with srcelement, standard browser with target and so on.
JQuery mainly uses Jquery.event class and JQuery.event.fix method to fix and package native event objects.
Copy Code code as follows:

jquery.event = function (src) {
Allow instantiation without the ' new ' keyword
if (!this.preventdefault) {
return new jquery.event (SRC);
}
Event Object
if (src && src.type) {
This.originalevent = src;
This.type = Src.type;
Events bubbling up the document may have been marked as prevented
By a handler lower down the tree; reflect the correct value.
this.isdefaultprevented = (src.defaultprevented | | | src.returnvalue = = FALSE | |
Src.getpreventdefault && src.getpreventdefault ()? Returntrue:returnfalse;
Event type
} else {
This.type = src;
}
TimeStamp is buggy to some events on Firefox (#3843)
So we won ' t rely on the native value
This.timestamp = Jquery.now ();
Mark it as fixed
this[Jquery.expando] = true;
};
function Returnfalse () {
return false;
}
function Returntrue () {
return true;
}
Jquery.event is based on DOM3 Events as specified by the ECMAScript Language
Http://www.w3.org/TR/2003/WD-DOM-Level-3-Events-20030331/ecma-script-binding.html
JQuery.Event.prototype = {
Preventdefault:function () {
this.isdefaultprevented = Returntrue;
var e = this.originalevent;
if (!e) {
Return
}
If Preventdefault exists run it on the original event
if (E.preventdefault) {
E.preventdefault ();
Otherwise set the ReturnValue property of the original event to False (IE)
} else {
E.returnvalue = false;
}
},
Stoppropagation:function () {
this.ispropagationstopped = Returntrue;
var e = this.originalevent;
if (!e) {
Return
}
If stoppropagation exists run it on the original event
if (e.stoppropagation) {
E.stoppropagation ();
}
Otherwise set the Cancelbubble property of the original event to True (IE)
E.cancelbubble = true;
},
Stopimmediatepropagation:function () {
this.isimmediatepropagationstopped = Returntrue;
This.stoppropagation ();
},
Isdefaultprevented:returnfalse,
Ispropagationstopped:returnfalse,
Isimmediatepropagationstopped:returnfalse
};

The Jquery.event class mainly did the following work

1, the Originalevent property is expanded, which temporarily saves the native event object.
2, fixes the TimeStamp, this property IE6/7/8 does not support, the other supports each browser The return value is also different.
3, block DOM element default behavior uniformly using Preventdefault
4, stop event bubbling unified use Stoppropagation
5, to achieve or expand the DOM3 event of several methods: Stopimmediatepropagation, isdefaultprevented, ispropagationstopped, isimmediatepropagationstopped

In addition, Jquery.event's writing style is also more unique. It creates an object using hidden new.
The JQuery.event.fix method is as follows

Copy Code code as follows:

Fix:function (event) {
if (event[Jquery.expando]) {
return event;
}
Store a copy of the original event object
and "clone" to set read-only properties
var originalevent = event;
event = jquery.event (originalevent);
for (var i = this.props.length, prop; i;) {
Prop = this.props[-I.];
event[prop] = originalevent[prop];
}
Fix target property, if necessary
if (!event.target) {
Fixes #1925 where srcelement might is defined either
Event.target = Event.srcelement | | Document
}
Check if Target is a textnode (Safari)
if (Event.target.nodeType = = 3) {
Event.target = Event.target.parentNode;
}
Add Relatedtarget, if necessary
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 && do C.clientleft | | Body && Body.clientleft | | 0);
Event.pagey = Event.clienty + (Doc && Doc.scrolltop | | body && Body.scrolltop | | 0)-(Doc && doc. ClientTop | | Body && Body.clienttop | | 0);
}
Add which for key events
if (Event.which = = null && (event.charcode!= null | | event.keycode!= null)) {
Event.which = Event.charcode!= null? Event.charCode:event.keyCode;
}
Add Metakey to Non-mac browsers (with Ctrl for PCs ' s and Meta for Macs)
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!== undefined) {
Event.which = (Event.button & 1 1: (Event.button & 2 3: (Event.button & 4 2:0));
}
return event;
},

It mainly did the following work
1,event = Jquery.event (originalevent); The sentence creates an instance object of the Jquery.event class, which is repaired and augmented just mentioned above.
2, a loop copies all the properties of the native event object to the event object in 1.
Copy Code code as follows:

for (var i = this.props.length, prop; i;) {
Prop = this.props[-I.];
event[prop] = originalevent[prop];
}

3, the Unified Event source object is target.
4, the Unified event related object is Relativetarget.
5, expanded the Pagex, Pagey, these two attributes were first introduced in Firefox. Browsers that do not support this property are computed using clientx/y.
6, expands the which, uses it to obtain the keyboard key value (KeyCode). This attribute is also introduced in Firefox.
7, repaired the Metakey.
8, expands the which, uses it to obtain the mouse key value
Careful people may notice that jquery gets the keyboard key value and the mouse button value are all using which. It does not have the same properties as other attributes. The standard (button) of the consortium is already available. That's what I read in jquery Seven and the differences in the mouse button values in each browser are analyzed in detail.
Finally, add the relevant code for the wrapper event object to Zchain.js.
Zchain-0.7.1.js

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.