JQuery Event Add [source analysis]

Source: Internet
Author: User

/* Helper functions for managing events – not part of the public interface. * Props to Dean Edwards ' addevent Library for many of the ideas. */jquery.event = {Add:function (elem, types, handler, data, selector) {var elemdata, Eventhandle, Events,t, TNS, type, n Amespaces, Handleobj,handleobjin, quick, handlers, special;//Don ' t attach events to NoData or Text/comment nodes (allow P Lain objects Tho) if (elem.nodetype = = = 3 | | elem.nodetype = = 8 | |!types | |!handler | |! ( Elemdata = Jquery._data (elem)) {return;} Caller can pass in a object of custom data in lieu of the handler//here in the past has not understood why to do so, the reason is that the handler here can be a function, that we Usually said the binding event method//can also be an event object, that is, the following handleobj, then if it is inside the jquery can be passed an event object come over if (handler.handler) {Handleobjin = Handler;handler = Handleobjin.handler;selector = Handleobjin.selector;}  Make sure, the handler have a unique ID, used to find/remove it later//assign Guidif (!handler.guid) {handler.guid = jquery.guid++;} Init the element ' s event struCture and main handler, if this is the first//from the caching system, gets the events array from the Cache event object, events = Elemdata.events;if (!events) {elemdata. Events = events = {};} The main listener function, the only method that binds to a DOM element, invokes dispatch assignment Event Eventhandle = elemdata.handle;if (!eventhandle) {elemdata.handle =  Eventhandle = function (e) {//Discard the second event of a jQuery.event.trigger () and//when an event was called after a Page has unloaded//here mainly to prevent the trigger manually triggered when the two times bubbling. Here in the trigger, there will be simulated bubbling events,//mainly for events that cannot be bubbling, such as: Focus can only simulate the bubbling event//If it has bubbled up, then there is no need to execute again. Return typeof jQuery!== "undefined" && (!e | | jQuery.event.triggered!== e.type)? jQuery.event.dispatch.apply (Ev Enthandle.elem, arguments): undefined;};/ /Add Elem as a property of the handle FN to prevent a memory leak with IE non-native events//here are mainly for IE memory problems eventhandle.el em = Elem;} Handle multiple events separated by a space//jQuery (...). Bind ("MouseOver mouseout", FN);//Cut multiple event combinations types = Jquery.trim (Hoverhack (types)). Split (""); for (t = 0; T < Types.length; t++) {//Use regular cutnamespace//rtypenamespace =/^ ([^\.] *)? (?:\. (.+))? $/,//such as: Click.namespace,hover.namespace//==> ["Click.namespace", "click", "namespace", index:0, Input: " Click.namespace "] TNS = Rtypenamespace.exec (types[t]) | | [];type = Tns[1];namespaces = (Tns[2] | | ""). Split ("."). Sort ();//If event changes its type with the special event handlers for the changed type//here mainly fixes the event to see if there is a definition in special, if Yes, I have just replaced the event type in special//mainly some native events, there is a compatibility problem with the browser, so we need to replace Special = jquery.event.special[Type] | | {};//If selector defined, determine special event API type, otherwise given type//here selector is a proxy event, if there is selector, then there is a bubbling thing Pieces//For delegatetype is mainly to replace the original can not bubble events such as: Focus and focusin//Then if the native event is not a problem for bubbling, then the detection of the binding type needs to be fixed: such as: MouseOver ==> MouseEnter type = (selector? special.delegateType:special.bindType) | | type;//Update Special based on newly reset typespecial = jquery.event.special[Type] | | {};//Handleobj is passed to all event Handlershandleobj = Jquery.extend ({type:type,origtype:tns[1],data:data,Handler:handler,guid:handler.guid,selector:selector,quick:selector && Quickparse (selector), namespace: Namespaces.join (".")}, Handleobjin);//Init the event handler queue if we ' re the firsthandlers = events[type];if (!han  dlers) {handlers = events[type] = [];handlers.delegatecount = 0;//only use addeventlistener/attachevent if the special Events Handler returns Falseif (!special.setup | | special.setup.call (elem, data, namespaces, eventhandle) = = = = False) {//Bind the global event handler to the Elementif (Elem.addeventlistener) {Elem.addeventlistener (type, Eventhandle, FA LSE);} else if (elem.attachevent) {elem.attachevent ("on" + Type, eventhandle);}}} if (special.add) {Special.add.call (Elem, handleobj); if (!handleobj.handler.guid) {handleObj.handler.guid = Handler.g UID;}} Add to the element's handler list, delegates in Frontif (selector) {handlers.splice (handlers.delegatecount++, 0, Han Dleobj);} else {Handlers.push (handleobj);} Keep track OF which events has ever been used, for event optimizationjquery.event.global[type] = true;} Nullify elem to prevent memory leaks in Ieelem = null;}

JQuery Event Add [source analysis]

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.