Meaning of the namespace for JQuery events

Source: Internet
Author: User
Tags event listener

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

Related Article

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.