JS and jquery Event delegation

Source: Internet
Author: User

Reference: http://www.ituring.com.cn/article/467


What is an event delegate: Popular, the event is onclick,onmouseover,onmouseout, and so is the event, entrusted, is to let others do, this event is added to some elements, but you add to others to do, complete this event.
For example: Three colleagues are expected to receive a courier in Monday. For the Signature Express, there are two ways: one is three people at the door of the company and other courier, the second is entrusted to the front desk mm for sign. In reality, most of US use commissioned programs (the company will not tolerate so many employees standing at the door to wait for courier). The front desk mm received the courier, she will determine who the recipient, and then according to the recipient's request for sign, or even on behalf of payment. This solution also has an advantage, that is, even if the company's new employees (regardless of how much), the front desk mm will be sent to the new staff after The courier to verify and sign on behalf of.


Using the bubbling principle, the event is added to the parent, triggering the execution effect.


1. Better performance
2. For newly created elements, you can have an event directly

Event Source:

Same as this (he does not have to look at the problem, who is the person who operates), the event object under the

Usage scenarios:

• Bind the same events for many elements in the DOM;
• Bind events to elements that do not already exist in the DOM;

JS's event delegate

Event Delegation for jquery

 $ (function   () {$ ( ' #ul1, #ul2 '). Delegate (' Li ', ' click ', function   () { Span style= "color: #0000ff;" >if  (!$ ( Span style= "color: #0000ff;"         >this ). css (' background ', ' Red '  true  );   else   {$ ( this ). css (' background ', ' #fff '  ); }     }) });

The newest on () method takes the delegate () method

$(function(){         $(' #ul1, #ul2 '). On (' Click ', ' Li ',function(){                 if(!$( This). attr (' s ')) {                 $( This). CSS (' background ', ' red '); $( This). attr (' s ',true); }Else {                         $( This). CSS (' background ', ' #fff '); $( This). Removeattr (' s '); }         }) });

JS and jquery Event delegation

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.