Example of jquery event name and namespace

Source: Internet
Author: User
Tags bind

Objective

In the official jquery API, there are some descriptions of namespaces, the address is: http://api.jquery.com/on/There are some English introduction, the topic is "Event names and namespaces" below some introduction. If some friends are not very understanding, I am here to introduce a simple!

jquery Event Namespaces

Let's look at some code first:

$ ("#haorooms"). On ("click.a", function () {});
$ ("#haorooms"). On ("Click.a.bb", function () {});
$ ("#haorooms"). On ("dbclick.a", function () {});
$ ("#haorooms"). On ("Mouseover.a", function () {});
$ ("#haorooms"). On ("Mouseout.a", function () {});
Of course, we can also use BIND for event binding. We see the code above, we can add our name to the event, and the event namespace. The event namespace is the event type that appends an alias with the point syntax to refer to the event, such as "click.a", where "a" is the alias of the current event type, the event namespace.

Suppose we want to delete the following namespaces:

$ ("#haorooms"). On ("Click.a.bb", function () {});
We can use:

$ ("#haorooms"). Off ("click.a.bb");//Delete the BB namespace "recommended" directly
$ ("#haorooms"). Off (". BB"); Delete the BB namespace "recommended" directly
$ ("#haorooms"). Off (". a"); Delete all the subspaces under the. A namespace, including. A.bb. a.cc, etc.,. A is the parent of. BB, so. A will be deleted below.
$ ("#haorooms"). Off ("click");//Directly unzip click, the following namespace will be deleted.
To be aware of:

If we write the following code:

$ ("#haorooms"). On ("click", Function () {});
$ ("#haorooms"). On ("click.a", function () {});
$ ("#haorooms"). On ("Click.a.bb", function () {});
Then we have to use trigger trigger Click event, that is, trigger the first, not the CLICK.A and click.a.bb are triggered, then how to solve this problem, I just want to trigger click, and do not trigger click.a and the following namespaces?

Never mind! The following solutions are available:

If an exclamation point is appended to the event type, a specific event type that does not contain a namespace is triggered.

If we just want to trigger click, we can write this:

$ ("#haorooms"). Trigger ("click!")
Only trigger BB, you can write this:

$ ("#haorooms"). Trigger ("click.a.bb");
With the namespace, it is convenient for us to do management on the same event!!!

Custom events

I don't have much to describe here! All custom events can be triggered through the jquery method, for example, the following example customizes a delay event type, binds it to an INPUT element object, and then triggers a custom event in the button click event.

$ ("input"). Bind ("Delay", function (event) {
settimeout (function () {
alert (Event.type);
},1000);
});
$ ("Input"). Click (function () {
$ ("input"). Trigger ("delay"); Triggering custom Events
});
Custom events are not really events, they can be interpreted as custom functions, and triggering custom events is equivalent to calling custom functions.

Through the above introduction, I do not know that the jquery event name and namespace have a deeper understanding of it! Look forward to your message exchange!

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.