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