Code for binding event processing functions to elements dynamically added by jquery

Source: Internet
Author: User

I used to manually bind the event handler when I added it. However, this function has been added to the new jquery version. We don't need to worry about it anymore.
Reference: http://api.jquery.com/live/
Previously, we defined events. For example, we wrote a click event for the element definition as follows: CopyCodeThe Code is as follows: $ ('input'). Click (function (){
// Process the code
});

OrCopy codeThe Code is as follows: $ ('. clickme'). BIND ('click', function (){
// Bound handler called.
});

However, this can only be used to define events for loaded elements. elements that are inserted later must be bound separately. Even if you use jquery's clone function, it cannot replicate events (so far, I still have no idea why it is defined as so, whether it cannot be copied or deliberately processed, to prevent some exceptions, it is still necessary to analyze the jquerySource code).
Now, you can easily use live,
$ ('. Clickme '). live ('click', function () {// live handler called .}); in this way, even if you insert the elements dynamically behind it, it will be bound to an event, $ ('body '). append ('<Div class = "clickme"> another target </div> ');

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.