Read jquery 12 Delete Event Core method _jquery

Source: Internet
Author: User

. Remove is just the opposite of what was mentioned in the previous article. and corresponds to the processing code one by one in. Add, that is, how many ways to add events in. Add. Remove has the corresponding deletion method.

The remove defines four parameters Elem, types, Handler, Pos. Literally, the meaning of four parameters is clear.

Elem for HtmlElement

types is of type string, event name such as ' click ' or ' mouseover mouseout '

handler is a function type, event callback function

POS is number type, specifying array position

But. Remove interior is not so simple, such as
1,handler Sometimes the type false, the handler is assigned to another function (the same as. Add).

Copy Code code as follows:

if (handler = = False) {
handler = Returnfalse;
}

2, types sometimes for an object, when the real handler is Types.handler,types is Types.type.
Copy Code code as follows:

Types is actually a event object here
if (types && types.type) {
handler = Types.handler;
types = Types.type;
}

We know that variable naming is meaningful and avoids misleading. In this sense, there are a lot of such writing in jquery, and a variable often has many meanings, obscure and difficult to read. As types here, it should be a string type, but the actual internal typeos is also handled for the object type. This is the reason that JS has no type check. In turn, this language will be more flexible, jquery is so compact, cohesive.
Gossip less, and see. What the Remove method does.
1. When only Elem is passed, all the events added on Elem will be deleted. such as $ (' #id '). Unbind ()
2. When types is string, and the event at the beginning of the point number (.) is deleted. such as $ (' #id '). Unbind ('. Name '). will add click.name,mouseover.name and so on are deleted.
The corresponding code is as follows
Copy Code code as follows:

Unbind all events for the element
if (!types | | typeof types = = "string" && types.charat (0) = = ".") {
types = Types | | "";
For (type in events) {
JQuery.event.remove (Elem, type + types);
}
Return
}

We found that in is a recursive call.
If so called
JQuery.event.remove (el, ' click ', fn)
Then it's not going to go to the side of the recursive, but directly into the while loop
Copy Code code as follows:

while ((type = types[i++])) {
...
}

This is the standard process for deleting events. The approximate steps are as follows
1, to determine whether the event name has a namespace (to distinguish between dots), if there is no namespace, delete all events under the event name. Otherwise, only an event in the namespace is deleted.
2. Gets the event array (EventType = events[type]), and if no handler is passed, all hanlder of the type event is deleted, otherwise only the specified handler of the event type is deleted.
3, for special events (such as live) processing
4, the final processing of elemdata, if the events are empty objects are deleted Elemdata events and handle attributes. Such as
Copy Code code as follows:

Remove the expando if it ' s no longer used
if (Jquery.isemptyobject (events)) {
var handle = Elemdata.handle;
if (handle) {
Handle.elem = null;
}
Delete elemdata.events;
Delete Elemdata.handle;
if (Jquery.isemptyobject (Elemdata)) {
Jquery.removedata (Elem, Undefined, true);
}
}

jquery Event Management data structure diagram:

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.