The usage and difference of delegate and on in jquery

Source: Internet
Author: User

 jquery1.7. Delegate () has been replaced by. On (), and here's an example to introduce the usage and distinction of delegate and on in jquery, and interested friends can refer to the following

In jQuery1.7. Delegate () has been replaced by. On (). For earlier versions, it still uses the most effective means of event delegation.   in event bindings and delegates, Delegate () and on in general, the two methods are equivalent.    . Delegate () the element specified (the child element of the selected element) adds one or more event handlers and specifies the function to run when these events occur.   Code as follows://jquery 1.4.3+  $ (elements). Delegate (selector, events, data, Handler); //jquery 1.7+  $ (elements). On (events, [selector], data, handler);    For example:. Delegate () code:  code is as follows: $ ("table"). Delegate (" TD "," click ", Function () {  alert (" Hello "); });   . On () code:  code is as follows: $ (" table "). On (" click "," TD ", function () {  alert (" Hi "); });    PS: The difference is seleter and events in different order   delegate and on method the child elements of the selected element must be The "legal" child element. For example   Copy code code as follows: $ ("table"). Delegate ("button", "click", Function () {...});   $ ("table"). On ("click", "P", function () {...});     does not work because the table child element should normally be tr,td...    on (EVENTS,[SELECTOR],[DATA],FN), and the parameter [selector] is optional,  A selector string is used for the descendants of the selector element of the filter's triggering event.   For example:  code is as follows: $ ("table"). On ("click", ". Td1", function () {  Alert ("HI"); });    Filter class is TD1 the table child element     and delegate selector is required.    
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.