When we use jquery to dynamically load HTML elements, but the elements are bound to events such as click, which are not valid for the newly added elements, how should they be resolved?
Live method
Live (Type, fn)
The new method in JQuery 1.3. Binds an event handler function (such as a click event) to all current and future matching elements. Custom events can also be bound.
Currently supports click, DblClick, MouseDown, MouseUp, MouseMove, MouseOver, Mouseout, KeyDown, KeyPress, KeyUp.
Not yet supported Blur, Focus, MouseEnter, MouseLeave, change, submit
Unlike bind (), Live () can only bind one event at a time.
This approach is similar to traditional bind, except that binding events with live will bind events to all current and future elements on the page (using delegation). For example, if you bind the click event to all Li on the page with live. Then, when you add an Li to this page later, the Click event is still available for this newly added Li. Instead of having to re-bind events to this newly added element. For example, the following code.
On method
But with the jquery version upgrade, since jquery 1.9 and above have been unable to use live, then there is no way, no, for the jQuery1.9 version and above can use on, the effect is equivalent to live. How to use the following on
1234567 |
<script>$(document).On("Click",". Waiting-save",function(){ $(This).html @T (" Saving ... " ") $ (). attr disabled" , ) (this) ; };</script> |
This article link address: resolves an invalid method for element events added using jquery with append
Resolves an invalid method for element events added using jquery with append