For example, when making an ajax to read the message list, there is a reply button after each message, the class is "reply", if you use $(".reply").click(function(){ //do something... }), presumably the reply button in the list loaded by ajax later, the click event will be invalid. In fact, the easiest way is to write onclick="" directly in the tag, but writing this is actually a bit low, and the best way is to bind a click event to the class name. There are two ways to solve the problem that dynamically added element nodes in jquery cannot trigger events, as follows: For better presentation, let's say you have code with the following structure under the body of a certain page:
Method 1: Use live The live() function binds one or more event handlers to the selected element and specifies which functions to run when those events occur. The live() function applies to matching the current and future elements of the selector. For example, elements created dynamically through scripts. The implementation is as follows:
Method 2: Use on You can bind an event to its parent or body through the on method, as follows:
Now you can try to see if the problem has been solved, I hope this article can really help you.
The second one I use is to bind the parent level element...
|