The jquery on () method binds multiple selectors, multiple events

Source: Internet
Author: User

On (EVENTS,[SELECTOR],[DATA],FN)

events: One or more space-delimited event types and optional namespaces, such as "click" or "Keydown.myplugin".
selector: A selector string is used for the descendants of the selector element of the filter's triggering event. If the selector is NULL or omitted, when it reaches the selected element, the event is always triggered.
data: To pass Event.data to the event handler when an event is triggered.
fn: The function to execute when the event is triggered. The value of false can also be abbreviated as a function, returning false.


Such a requirement, if implemented using the live () method is very simple and easy to understand.

The code is as follows Copy Code

$ (' Nav li, #sb-nav Li, #help li '). Live (' click ', function () {
Code ...
});

After the 1.7 release, jquery recommends using the on method instead of the previous bind, live, and delegate methods.
How do you write the above sentence if you use on?

In fact, to view the live source, Live is actually commissioned Doucment to do event delegation.

In this way, you can bind the on method to the document.

The code is as follows Copy Code

$ (document). On (' Click ', ' #header. Fixed-feedback-bn, #sb-sec. Feedback-bn ', function () {
Code ...
});

There is also a situation in which the on () method binds multiple events, which can be written like this:

The code is as follows Copy Code

$ ("Table.planning_grid"). On ({
Mouseenter:function () {
Handle MouseEnter ...
},
Mouseleave:function () {
Handle MouseLeave ...
},
Click:function () {
Handle Click ...
}
}, "TD");


Finally, you bind multiple selectors with the on () method, and multiple events can be written like this:

The code is as follows Copy Code
$ (document). On ({
Mouseenter:function () {
Handle MouseEnter ...
},
Mouseleave:function () {
Handle MouseLeave ...
},
Click:function () {
Handle Click ...
}
}, ' #header. Fixed-feedback-bn, #sb-sec. feedback-bn ');

Related articles

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.