This article mainly introduces the JS delegate and live, the need for friends can refer to the
There are two methods in jquery that can be used to bind automatically appended DOM objects, which are live and delegate, and in fact, these two methods are a variant of the Bind method, and we usually use bind when it comes to fixed DOM objects. And the object dynamically generated DOM objects, using bind can do nothing, then live and delegate on the appearance, hehe. Live method, used to bind a (Class) object, bind the method code as follows: //live $ ("TD"). Live ("click", Function () { Alert ($ (this). html (); &NBSP ; }); //below also can be $ ("#list TD"). Live ("Click", Function () { alert ($ (this). html ()); &NB Sp }); Delegate method, used to bind a (Class) object under the child object, to bind the child object method (Delegate child object, let the child object has some method, hehe) code is as follows: $ ("#list"). Delegate ("TD" , "click", Function () { , alert ($ (this). html (); &NBS P }); The following demo completion code: Code is as follows:; HTML lang= "en" xmlns= "http://www.w3.org/1999/xhtml" > <head> <meta charset= "Utf-8"/> <title></title> <script src= "jquery.js" type= "Text/javascript" ></ script> <script id= "ListTemplate" type= "text/html" > <tr> & nbsp <td>[UserID]</td> <TD>[USERIMG]&L t;/td> <td>[UserName]</td> </TR> ; </script> <script type= "Text/javascript" > var reg = NE W RegExp ("[[^[]]*]]", ' IgM '); I g M is used to specify case-sensitive matching, global matching, and multiline matching, respectively. $ (function () { //live & nbsp $ ("#list TD"). Live ("Click", Function () { &NBSP Alert ($ (this). html ()); }); $ ("#addFun"). Click (function () { &NBSP ; var html = document.getElementById ("ListTemplate"). InnerHTML; var source = Html.replace (reg, function (node, key) {return {' Us Erimg ': ' 1 ', ' UserName ': ' Zhang ', ' UserID ': ' 1 '}[key]; }); $ ("#list"). Append (source); }); }); </script> </head> <body> <div id= "comment_ul_2" > </D iv> <input type= "button" id= "Addfun" value= "click Me"/> <table id= "list" border= " 1 "> <tbody> </tbody> ≪/table> </body> </html>