Read jquery 13 core methods for adding events and deleting events _jquery

Source: Internet
Author: User
Tags uuid jquery library
The JQuery event module relies heavily on its data store (Jquery.data), and you will find that the DataManager object in my code corresponds to it.
Only the bind and Unbind methods are provided here. Temporarily does not contain
1, Events namespace (event namespace)
2, Event agent (delegation)
3, special events such as Dom ready
The interface is as follows:
Copy Code code as follows:

E.bind (el, ' click ', FN);
E.bind (el, ' click ', FN, data);
E.unbind (el, ' click ', FN);
E.unbind (el, ' click ');
E.unbind (EL);

Copy Code code as follows:

/**
* Event from JQuery
* 2011-06-20 Snandy
*
* A number of helper functions used for managing events.
* Many of the ideas behind this code originated from JQuery Library (1.6.2).
*
* Example
*
* E.bind (EL, ' click ', FN);
*
* E.bind (EL, ' click ', FN, data);
*
* E.unbind (EL, ' click ', FN);
*
* E.unbind (EL, ' click ');
*
* E.unbind (EL);
*
*/
E = function (window) {
var uuid = 0,
Globalcache = {},
Doc = Window.document,
The Consortium =!! Doc.addeventlistener,
expando = ' Snandy ' + (' +math.random ()). Replace (/\d/g, '),
AddListener = The consortium?
Function (el, type, fn) {El.addeventlistener (type, FN, false);}:
Function (el, type, fn) {el.attachevent (' on ' + type, fn);},
RemoveListener = The consortium?
Function (el, type, fn) {El.removeeventlistener (type, FN, false);}:
Function (el, type, fn) {el.detachevent (' on ' + type, fn);
Dispatch = The consortium?
Function (el, type) {
try{
var evt = doc.createevent (' Event ');
Evt.initevent (Type, true, true);
El.dispatchevent (EVT);
}catch (e) {alert (e)};
} :
Function (el, type) {
try{
El.fireevent (' on ' + type);
}catch (e) {alert (e);}
},
DataManager = {
Data:function (elem, name, data) {
var getbyname = typeof name = = "String",
Thiscache,
Isnode = Elem.nodetype,
Cache = Isnode? Globalcache:elem,
id = isnode? elem[expando]: elem[expando] && expando;
if (!id && isnode) {
Elem[expando] = id = ++uuid;
}
if (!cache[id]) {
Cache[id] = {};
}
Thiscache = Cache[id];
if (data!== undefined) {
Thiscache[name] = data;
}
Return getbyname? Thiscache[name]: Thiscache;
},
Removedata:function (Elem, name) {
var id = Elem[expando],
Thiscache = Globalcache[id];
if (!id | |!thiscache) {
Return
}
if (typeof name = = = ' String ') {
Delete Thiscache[name];
}else{
Delete Globalcache[id];
}
}
};
function Returnfalse () {
return false;
}
function Returntrue () {
return true;
}
function Now () {
Return (new Date). GetTime ();
}
function Isemptyobject (obj) {
for (var i in obj) {
return false;
}
return true;
}
function Addevent (elem, types, handler, data) {
if (Elem.nodetype = = 3 | | elem.nodetype = = 8) {
Return
}
if (handler = = False) {
handler = Returnfalse;
else if (!handler) {
Return
}
var elemdata = Datamanager.data (Elem),
Events = Elemdata.events,
Eventhandle = Elemdata.handle,
types = Types.split ("");
if (!events) {
Elemdata.events = events = {};
}
if (!eventhandle) {
Elemdata.handle = Eventhandle = function (e) {
Return Evthandle.call (Eventhandle.elem, E);
};
}
Eventhandle.elem = Elem;
var type, i = 0;
while (type = types[i++]) {
var handleobj = {Handler:handler, data:data},
handlers = Events[type];
if (!handlers) {
Handlers = Events[type] = [];
AddListener (Elem, type, eventhandle);
}
Handlers.push (Handleobj);
}
Elem = null;
}
function Evthandle (event) {
event = fixevent (Event | | window.event);
var handlers = (Datamanager.data (This, "events") | | {}) [Event.type] | | []). Slice (0);
Event.currenttarget = this;
for (var j = 0, L = handlers.length J < L; j + +) {
var handleobj = handlers[j];
Event.handler = Handleobj.handler;
Event.data = Handleobj.data;
Event.handleobj = Handleobj;
var ret = HandleObj.handler.call (this, event);
if (ret!== undefined) {
if (ret = false) {
Event.preventdefault ();
Event.stoppropagation ();
}
}
if (event.isimmediatepropagationstopped ()) {
Break
}
}
}
function Removeevent (elem, types, handler) {
Don ' t does events on text and comment nodes
if (Elem.nodetype = = 3 | | elem.nodetype = = 8) {
Return
}
if (handler = = False) {
handler = Returnfalse;
}
var type, origtype, i = 0, J,
Elemdata = Datamanager.data (Elem),
Events = Elemdata && elemdata.events;
if (!elemdata | |!events) {
Return
}
Unbind all events for the element
if (!types) {
types = Types | | "";
For (type in events) {
Removeevent (Elem, type);
}
Return
}
Handle multiple events separated by a
JQuery (...). Unbind ("MouseOver mouseout", FN);
types = Types.split ("");
while ((type = types[i++])) {
Origtype = type;
Handleobj = null;
EventType = events[type];
if (!eventtype) {
Continue
}
if (!handler) {
for (j = 0; J < Eventtype.length; J + +) {
Handleobj = eventtype[J];
Removeevent (Elem, Origtype, Handleobj.handler);
Eventtype.splice (J--, 1);
}
Continue
}
for (j = 0; J < Eventtype.length; J + +) {
Handleobj = eventtype[J];
if (handler = = Handleobj.handler) {
Remove the given handler for the given type
Eventtype.splice (J--, 1);
}
}
}
Remove generic event handler if no more handlers exist
if (Eventtype.length = = 0) {
Delete events[Origtype];
}
Remove the expando if it ' s no longer used
if (Isemptyobject (events)) {
var handle = Elemdata.handle;
if (handle) {
Handle.elem = null;
}
Delete elemdata.events;
Delete Elemdata.handle;
if (Isemptyobject (Elemdata)) {
Datamanager.removedata (Elem, ' events ');
}
}
}
function Event (SRC) {
This.originalevent = src;
This.type = Src.type;
This.timestamp = Now ();
}
Event.prototype = {
Preventdefault:function () {
this.isdefaultprevented = Returntrue;
var e = this.originalevent;
if (E.preventdefault) {
E.preventdefault ();
}
E.returnvalue = false;
},
Stoppropagation:function () {
this.ispropagationstopped = Returntrue;
var e = this.originalevent;
if (e.stoppropagation) {
E.stoppropagation ();
}
E.cancelbubble = true;
},
Stopimmediatepropagation:function () {
this.isimmediatepropagationstopped = Returntrue;
This.stoppropagation ();
},
Isdefaultprevented:returnfalse,
Ispropagationstopped:returnfalse,
Isimmediatepropagationstopped:returnfalse
};
function Fixevent (evt) {
var props = "Altkey attrchange attrname Bubbles button cancelable charcode clientX clientY ctrlkey currenttarget data deta Il eventphase fromelement handler keycode Layerx layery metakey newvalue offsetX offsetY originaltarget Pagex pagey Lue relatednode relatedtarget screenX screenY shiftkey srcelement target toelement view Wheeldelta which ". Split (" "),
len = props.length;
var originalevent = evt;
EVT = new Event (originalevent);
for (var i = len, prop I;) {
Prop = props[-I.];
evt[prop] = originalevent[prop];
}
if (!evt.target) {
Evt.target = Evt.srcelement | | Document
}
if (Evt.target.nodeType = = 3) {
Evt.target = Evt.target.parentNode;
}
if (!evt.relatedtarget && evt.fromelement) {
Evt.relatedtarget = Evt.fromelement = = Evt.target? Evt.toElement:evt.fromElement;
}
if (Evt.pagex = = null && evt.clientx!= null) {
var doc = document.documentelement, BODY = document.body;
Evt.pagex = Evt.clientx + (Doc && Doc.scrollleft | | body && Body.scrollleft | | 0)-(Doc && doc.cl Ientleft | | Body && Body.clientleft | | 0);
Evt.pagey = Evt.clienty + (Doc && Doc.scrolltop | | body && Body.scrolltop | | 0)-(Doc && Doc.clie Nttop | | Body && Body.clienttop | | 0);
}
if (!evt.which && (Evt.charcode | | evt.charcode = = 0) (Evt.charCode:evt.keyCode)) {
Evt.which = Evt.charcode | | Evt.keycode;
}
if (!evt.metakey && evt.ctrlkey) {
Evt.metakey = Evt.ctrlkey;
}
if (!evt.which && evt.button!== undefined) {
Evt.which = (Evt.button & 1 1: (Evt.button & 2 3: (Evt.button & 4 2:0));
}
return evt;
}
function bind (EL, type, FN, data) {
var handler;
if (typeof type = = = "Object") {
for (var key in type) {
Bind (EL, Key, Type[key], data);
}
Return
}
Handler = FN;
Addevent (el, type, handler, data);
}
function unbind (EL, type, fn) {
if (typeof type = = = "Object") {
for (var key in type) {
Unbind (EL, Key, Type[key]);
}
}else {
Removeevent (EL, type, fn);
}
}
return {
Data:dataManager.data,
RemoveData:dataManager.removeData,
Bind:bind,
Unbind:unbind
};
} (this);

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.