JQuery on () method binds a dynamic element's Click event

Source: Internet
Author: User

Has been plagued by this problem before, after the jQuery1.7 version of the Add on method, previously learned, its superiority than live (), bind (), delegate () and other methods, before this project to use this to test the results found, Incredibly dynamically generated tags clicked did not respond, and live method can support, and so everywhere search information, ask netizens, results for a long time in an article finally found the answer ...

When JQuery uses on to bind dynamically generated elements, it cannot manipulate the object directly, but instead chooses its non-dynamically generated parent node and then finds itself to achieve the effect. Everybody look at the source code to know. The generated button cardinality entry on method is valid by clicking the invalid Live method. UI front-end frame latest masterpiece! Prize Probation!

For example, there are two elements below the page:

. Code
  1. <input type= "button" Name= "addbtn" value= "button Add"/>
  2. <div id= "Test" ></div>

Using the jquery code below you can compare the differences:

. Code
  1. $ (function () {
  2. var a = 1,
  3. $_div = $ (' #test ');
  4. $ (' input[name=addbtn] '). On (' click ', function () {
  5. $_div.append (' <input type= ' button "name=" Test ' + A + ' "value=" buttons ' + A + ' "/> ');
  6. a++;
  7. });
  8. Even item click events
  9. $_div.on (' Click ', ' Input[name^=test]:even ', function () {
  10. Alert (' I am a valid on method, can you see me: ' + this.value);
  11. });
  12. An odd-bound Click event finds the click is invalid, but instead uses the live method to support
  13. $ (' input[name^=test]:odd '). On (' click ', function () {
  14. Alert (' I am invalid on method, you cannot see me ');
  15. });
  16. An odd-bound Click event finds the click is invalid, but instead uses the live method to support
  17. $ (' input[name^=test]:odd '). Live (' click ', function () {
  18. Alert (' I am the live method, can you see me: ' + this.value);
  19. });
  20. });

The code is simple, do not put the demo page, if there is nothing to understand, you can leave a message. UI front-end frame latest masterpiece! Prize Probation!

JQuery on () method binds a dynamic element's Click event

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.