The jquery on () method binds multiple selectors and events.

Source: Internet
Author: User

This article describes how to bind multiple selectors and events to the jquery on () method. I hope some methods will be helpful to you.

On (events, [selector], [data], fn)

• Events: one or more event types and optional namespaces separated by spaces, such as "click" or "keydown. myPlugin ".
• Selector: the descendant of a selector element used by a selector string to filter trigger events. If the selector is null or omitted, an event is always triggered when it reaches the selected element.
• Data: when an event is triggered, the event. data must be passed to the event processing function.
• Fn: The function executed when the event is triggered. The false value can also be abbreviated as a function, and false is returned.


Such a requirement can be easily understood and implemented using the live () method.

The Code is as follows: Copy code

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

After jquery version 1.7, we recommend that you use the on method instead of the previous bind, live, and delegate methods.
How can I write the above sentence if I use on?

In fact, you can see from the live source code that live actually delegates events to doucment.

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...
});

In another case, the on () method binds multiple events and can be written as follows:

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, bind multiple selectors using the on () method, and write multiple events as follows:

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.