The events explanation for jquery's on is one or more space-delimited event types and optional namespaces, or just namespaces, such as "click", "Keydown.myplugin", or ". Myplugin".
What does the namespace mean here?
$ (' #element '). On (' click ', dosomething). On (
Like the above-bound event listener, both DoSomething and Dosomethingelse will be triggered when the element is clicked. This is one of the conveniences of using jQuery, where you can add different listeners to the same event of an element at any time. Unlike the onclick, the new listener will overwrite the old ones.
If you want to unbind one of the listeners, like dosomething, what do you do?
Is that right?
Attention! This line of code will unbind all listeners of the element's Click event, which is not the result we want.
Fortunately, JQuery's. Off () method can accept the second parameter, just like. On (). Simply pass the name of the listener function as the second argument to the. Off () method to unbind the specified listener.
But if you don't know the name of the function, or you're using an anonymous function:
function () {Console.log (' dosomething '
How can I precisely unbind a single click event listener?
First on the code:
function () {Console.log (' dosomething '
Instead of just passing the click event as a parameter to the. On () method, it specifies a namespace for the click event and then listens for the click event in the namespace. At this point, even if the listener is an anonymous function, it is actually "well-known". Now you can unbind an event listener in a specific namespace like this.
Meaning of the namespace for JQuery events