Analysis of JS delegate and live

Source: Internet
Author: User
Tags bind

  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&GT ;     </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>  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.