jquery dynamically added element binding event handler function code _jquery

Source: Internet
Author: User
My approach at the time was to bind the event handler manually when I added it. But the new version of jquery has added this functionality. We don't have to worry about it anymore.
Reference: http://api.jquery.com/live/
Previously we defined events such as the Click event for an element definition to be written like this:
Copy Code code as follows:

$ (' input '). Click (function () {
Handling code
});

Or
Copy Code code as follows:

$ ('. ClickMe '). Bind (' click ', function () {
Bound handler called.
});

However, this can only be defined for an element that has already been loaded, and those elements that are added later will need to be bound separately. Even if you use jquery's clone function, it does not replicate events (so far I'm not sure why it is so defined, whether it can be copied or deliberately handled so as to prevent some exceptions, which has yet to be analyzed for jquery's source code).
Now, with live you can easily handle,
$ ('. ClickMe '). Live (' click ', function () {//Live handler called.}); In this way, even the elements that you insert dynamically later will be bound to the 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.