On the jquery event namespace

Source: Internet
Author: User

What is the event namespace for jquery?

Let's look at the following simple code:

$ ("#btn"). On ("click.name1.name2",function() {    console.log (1);  })

Where name1, Name2 is the namespace of the binding click event, where the namespace is followed by the event name with "." To connect. Namespaces can also be understood here as aliases, where the Click event bound here has two aliases name1 and name2.

How does binding an event with a namespace unbind?

Here are 8 ways to unbind the Click event of a namespace that was previously bound click.name1.name2:

1  $ ("#btn"). Off ("click"); 2  $ ("#btn"). Off (". name1"); 3  $ ("#btn"). Off (". name2"); 4  $ ("#btn"). Off (". Name1.name2"); 5  $ ("#btn"). Off ("click.name1"); 6  $ ("#btn"). Off ("click.name2"); 7  $ ("#btn"). Off ("click.name1.name2"); 8  $ ("#btn"). Off ("click.name2.name1");
9 $ ("#btn"). Off (". name2.name1");

1 will unbind all click events (including any namespaces);

2 Cancels all events (not just the Click event) that include name1 in the binding namespace;

3 Cancels all events (not just the Click event) that include name2 in the binding namespace;

4 Cancels all events (not just the Click event) that include name1 and name2 in the binding namespace;

5 cancels the Click event that includes name1 in the binding namespace;

6 cancels the Click event that includes name2 in the binding namespace;

7, 8 cancels the Click event in the binding namespace that includes name1 and name2 (NAME1 and name2 two namespaces are level, not parent and child relationships);

9 Cancels all events (not just the Click event) that include name1 and name2 in the binding namespace;

Combining these can be found in the namespace of the jquery event similar to the class name of the tag, so it is very good memory. What if you only want to unbind the click event without a namespace? It's actually very simple:

$ ("#btn"). Off ("click!");

Add "!" to the event name when you unbind it So that events with namespaces are not affected.

Ps

jquery can also bind and unbind custom events, and custom events can have namespaces.

On the jquery event namespace

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.