The usage and differences of delegate and on in jQuery

Source: Internet
Author: User

This article mainly introduces the usage and differences of delegate and on in jQuery in detail. If you need a friend, you can refer to it for help.

In jQuery1.7,. delegate () has been replaced by. on. For earlier versions, it still uses the most effective means of event delegation. In event binding and delegation, the delegate () and on methods are generally equivalent. Add one or more event handlers to the element specified by. delegate (), which belongs to the child element of the selected element, and specify the function to run when these events occur. The Code is as follows: // jQuery 1.4.3 + $ (elements ). delegate (selector, events, data, handler); // jQuery 1.7 + $ (elements ). on (events, [selector], data, handler); example :. delegate () code: the code is as follows: $ ("table "). delegate ("td", "click", function () {alert ("hello ");});. on () code: the code is as follows: $ ("table "). on ("click", "td", function () {alert ("hi") ;}); PS: the difference between the two is that the subelement of the selected element in different order of seleter and events must be a "legal" subelement. For example, copy the Code as follows: $ ("table "). delegate ("button", "click", function (){...}); $ ("table "). on ("click", "p", function (){...}); it does not work, because under normal circumstances, the table sub-element should be tr, td... on (events, [selector], [data], fn), the [selector] parameter is optional. A selector string is used to filter the descendant of the selector element used to trigger events. For example, the Code is as follows: $ ("table "). on ("click ",". td1 ", function () {alert (" hi ") ;}); filters table sub-elements whose class is td1 and selector of delegate is required.

Related Article

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.