jquery dynamically added element-bound event handler code

Source: Internet
Author: User

jquery dynamically added element binding event handler code font: [Increase decrease] Type: reprinted there was no use for jquery for a while, and today comes the problem. It was known that there were livejquery to solve. But I don't like to load another one for this.

My approach at the time was to manually bind the event handler when I added it. However, this feature has been added to the new jquery. We don't have to worry about this anymore.
Reference: http://api.jquery.com/live/
We previously defined events, such as defining a click event for an element, which is written like this:

Copy CodeThe code is as follows:
$ (' input '). Click (function () {
Working with Code
});


Or

Copy CodeThe code is as follows:
$ ('. ClickMe '). Bind (' click ', function () {
Bound handler called.
});


However, this can only be defined for elements that have already been loaded, and those that later add inserts need to be bound separately. Even if you use jquery's clone function, it does not replicate the event (so far I am not sure why it is so defined, it is not possible to copy or deliberately do so, to prevent some exceptions, it is still to analyze the source code of jquery).
Now, with live you can easily get it done,
$ ('. ClickMe '). Live (' click ', function () {//Live handler called.}); In this way, you will be bound to the event, $ (' body ') even after dynamically inserting the element in the back. Append (' <div class= ' ClickMe ">another target</div> ');

Excerpt from: http://www.jb51.net/article/27872.htm

jquery dynamically added element-bound event handler code

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.