简体   繁体   中英

what does jQuery.event.add() mean?

I'm trying to understand javascript code that i found in some website and i found this command
I've searched the internet a long time before coming here
there is nothing about it in the jquery API or w3school

this is how add was defined

var add = jQuery.event.add;

and this is an example of how the function was used

data = {
    target:e.target,
    startX:e.pageX,
    startY:e.pageY,
    pageX:e.pageX,
    pageY:e.pageY,
    timeStamp:e.timeStamp
};

add(document,mouseevents.move,mousemove,data);

I would love an explanation

jQuery.event.add() is used internally to add an event handler. For example it is called inside of .on() which is used to create event handlers. In fact, the very last lines of the implementation for .on() in jQuery 2.x and jQuery 3.x is like this:

    return this.each( function() {
        jQuery.event.add( this, types, fn, data, selector );
    });

So, this is adding an event handler to a DOM object and to jQuery internal data structures.

I do not believe that jQuery.event.add() is intended to be called outside of the jQuery implementation.

The only other place it is used in jQuery itself is inside a function that is cloning some objects and cloning their event handlers (it's used to define the new event handlers on the new cloned objects).

And, you can see the jQuery 3.x source for jQuery.event.add() here on Github and examination of that code confirms that it is adding an event listener and updating jQuery's internal data structures that keep track of event handlers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM