jquery Source Analysis--event Module (3)

Source: Internet
Author: User

The last thing left is a manual trigger for the event. jquery provides two functions trigger and Triggerhandler to manually trigger events that can trigger native events and custom events. This trigger will not only trigger a jquery bound event, but will also trigger the native inline binding event. The difference between trigger and Triggerhander is:
Trgger: JQuery.event.trrger will be called on all matching elements, and will bubble, triggering the default behavior of the browser. The return value is a jquery object
Triggerhandler: JQuery.event.trrger is called only on the first matched element and does not bubble and does not trigger the default behavior of the browser. is the return value of the function
Can be seen through the bottom of the JQuery.event.trrger tool method to achieve. How did the JQuery.event.trrger work?

  1. Constructs a bubbling path from the current node to the window, stored in an array.
  2. Iterate through this array of paths, invoking each node's jquery main listener function and the inline listener function of the element, without being prevented from bubbling.
  3. Finally, the default behavior is triggered with the native Event trigger function (Click,focus). and setting a jQuery.event.triggered flag to flag is to trigger the default behavior and avoid triggering the event again.
    Finally, the source code.
    Triggerfunction(Event, data, Elem, onlyhandlers) {//events can be event types, custom event objects, or jquery event objects        vari, cur, tmp, Bubbletype, Ontype, handle, special, Eventpath= [Elem | | document],//Bubble Road StrengthType = Hasown.call (event, "type")? Event.type:event,//determine if an event is an object (custom and jquery) or a type stringnamespaces = Hasown.call (Event, "namespace")? Event.namespace.split ("."): [];//name Spacecur= TMP = Elem = Elem | | Document//cur an ancestor element that points to the current element        //Don ' t do events on text and comment nodes        if(Elem.nodetype = = = 3 | | elem.nodetype = = 8) {//exclude text nodes and comment nodes            return; }        //Focus/blur morphs to focusin/out, ensure we ' re not firing them right now Rfocusmorph =/^ (?: Focusinfocus|focusoutb Lur) $/,        if(Rfocusmorph.test (type + jQuery.event.triggered)) {//filter out the default behavior of the Foces/blur event, followed by unified Focusin/focusout            return; }        if(Type.indexof (".") >= 0) {//resolving event types and namespaces and sorting namespaces            //namespaced trigger; Create a regexp to match event type in handle ()namespaces = Type.split (".")); Type=Namespaces.shift ();        Namespaces.sort (); } Ontype= Type.indexof (":") < 0 && "on" + type;//There are: no calls in inline listener events        //Caller can pass in a Jquery.event object, object, or just an Event type stringevent = event[Jquery.expando]?//determine if the jquery listener object is not created. Event:NewJquery.event (Type,typeofevent = = = "Object" &&event); //Trigger Bitmask: & 1 for Native handlers, & 2 for JQuery (always true)Event.istrigger = onlyhandlers? 2:3; Event.namespace= Namespaces.join ("."); Event.namespace_re= Event.namespace?NewRegExp ("(^|\\.)" + namespaces.join ("\ \ (?:. *\\.|)") + "(\\.| $)" ) :            NULL; //Clean up the event in case it is being reusedEvent.result = undefined;//The return value of the last function that has a return value        if(!event.target) {//Fix TargetEvent.target =Elem; }        //Clone any incoming data and prepend the event, creating the handler arg listdata = data = =NULL?//encapsulates additional data (if any) and event objects into an array. Easy to apply call[Event]: Jquery.makearray (data, [Event]); //Allow special events to draw outside the linesSpecial = jquery.event.special[Type] | | {}; if(!onlyhandlers && special.trigger && special.trigger.apply (elem, data) = = =false) {//the trigger function of the modified object is called first to trigger            return; }        //determine event propagation path in advance, per the events spec (#9951) structure bubbling Road strength        //Bubble up-to-document, then-to-window; watch for a global ownerdocument var (#9724)        if(!onlyhandlers &&!special.nobubble &&!jquery.iswindow (elem)) {//excludes onlyhandlers=true, load, and window objects.Bubbletype= Special.delegatetype | | Type//prioritize the properties of a remediation object            if(!rfocusmorph.test (Bubbletype + type)) {//initializes the cur as the parent element of the current element (excludes Focus/blur)Cur =Cur.parentnode; }             for(; cur; cur = cur.parentnode) {//TraverseEventpath.push (cur); TMP= cur;//tmp points to the topmost element that can be reached            }            //Only Add window if we got to document (e.g., not plain obj or detached DOM)            if(TMP = = = (Elem.ownerdocument | | document)) {//If the top level is document then add windowEventpath.push (Tmp.defaultview | | tmp.parentwindow | |window); }        }        //Fire handlers on the event pathi = 0;  while((cur = eventpath[i++]) &&!event.ispropagationstopped ()) {//traversing the bubbling path to trigger the listening functionEvent.type= i > 1?BubbleType:special.bindType||type; //JQuery HandlerHandle = (Data_priv.get (cur, "events") | | {}) [Event.type] && data_priv.get (cur, "handle");//Remove the main listener function            if(handle) {//calling the main listener functionhandle.apply (cur, data); }            //Native Handlerhandle = Ontype &&cur[Ontype]; if(Handle && handle.apply && jquery.acceptdata (cur)) {//perform in-line event listener functionsEvent.result =handle.apply (cur, data); if(Event.result = = =false) {//a return value of false means that the default behavior is blockedEvent.preventdefault (); } }} event.type= type;//Reply to type        //If Nobody prevented the default action, do it now        if(!onlyhandlers &&!event.isdefaultprevented ()) {//No default behavior is blocked            if((!special._default | | special._default.apply (EVENTPATH.POP (), data) = = =false) &&Jquery.acceptdata (Elem)) {                //Call A native DOM method on the target with the same name name as the event.                //Don ' t do default actions in window, that's where global variables Be (#6170)                if(Ontype && jquery.isfunction (elem[type]) &&!Jquery.iswindow (Elem)) {                    //Don ' t re-trigger an OnFoo event time we call its FOO () method avoids triggering the inline listener function againTMP =elem[Ontype]; if(TMP) {elem[Ontype]=NULL; }                    //Prevent re-triggering of the same event, since we already bubbled it above avoid triggering the main function againjQuery.event.triggered =type;                    elem[type] (); JQuery.event.triggered=undefined; if(TMP) {//Recoveryelem[Ontype] =tmp; }                }            }        }        returnEvent.result; },

jquery Source Analysis--event Module (3)

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.