We know that each method of binding events in jQuery has its corresponding method of removing event binding, such as off() corresponding to on(), unbind() corresponding to bind(), die() corresponding to live() , I am very curious about how this unbundling of anonymous events is achieved. The source code of jQuery is too esoteric and hard to understand. Can any expert post a simplified version of the code to analyze the implementation principle?
I think to understand off processing, you must first understand the on operation. I read the source code of jquery2.x last year, and the event aspect is quite complicated.
I looked through my rough notes and found that the video explaining the incident did not mention anything about it that I had to watch.
About binding events, you can combine the on source code implementation and the jquery.event.add method:
My understanding is that jquery mainly sets cache data cache for elements. The cache stores events variables (event callback queue collection), which are stored in the form of "event": "callback function array" to add multiple times to a certain DOM. When an event occurs, the callback can be triggered, but what is actually bound to the native event is the traversal execution of the callback function array.
As for off, let’s take a look at the source code part of off first:
When you see the last sentence, you know that it actually calls the
jQuery.event.remove
method.remove method
Mainly perform operations such as deleting event key-value pairs from the events variable stored in the cache when the element is previously on.
If it is just
$(xx).off('click')
, then it is to directly traverse and delete the callback function group corresponding to the click event in events. If the off parameter also passes a specific callback function, then it is Traverse and compare the callback array and delete the corresponding callback function...For the jquery source code, it is recommended to watch the video of Miaowei Classroom for the early basic part. For other information, you can watch Daniel's blog post at http://www.cnblogs.com/aaronj... or purchase similar books on jquery source code analysis.
The source code involves too many details, and I won’t be able to sort them out for a while = =, so I will express the general point... Please correct me if I have any misunderstandings~
The following is the code of on