What is the event delegate for jquery:
Event delegation is mainly realized by using event bubbling phenomenon, and the precise mastery of event delegation can improve the efficiency of code execution. Let's look at a code example:
<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"><Metaname= "Author"content= "http://www.51texiao.cn/" /><title>Ant Tribe</title><styletype= "Text/css">Table{width:300px;Height:60px;Background-color:Green;}Table TD{Background-color: White;}</style><Scripttype= "Text/javascript"src= "Mytest/jquery/jquery-1.8.3.js"></Script><Scripttype= "Text/javascript">$ (document). Ready (function(){ $("TD"). Bind ("Click",function(){ $( This). Text ("haha"); })})</Script></Head><Body><Tablecellspacing= "1"> <TR> <TD>Cell</TD> <TD>Cell</TD> <TD>Cell</TD> <TD>Cell</TD> <TD>Cell</TD> </TR> <TR> <TD>Cell</TD> <TD>Cell</TD> <TD>Cell</TD> <TD>Cell</TD> <TD>Cell</TD> </TR></Table></Body></HTML>
In the preceding code, a click event handler is bound to each TD using the BIND () method, so that when the cell is clicked, the text in the cell is reset. Although this method achieves the desired effect and looks very perfect, it is not so, if the cell is very many times, traversing the cell and binding the event handler for each cell will greatly reduce the performance of the code, if the parent element of the cell listens to the event, Just determine if the DOM element that originally triggered the event is TD.
The code is modified as follows:
<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"><Metaname= "Author"content= "http://www.51texiao.cn/" /><title>Ant Tribe</title><styletype= "Text/css">Table{width:300px;Height:60px;Background-color:Green;}Table TD{Background-color: White;}</style><Scripttype= "Text/javascript"src= "Mytest/jquery/jquery-1.8.3.js"></Script><Scripttype= "Text/javascript">$ (document). Ready (function(){ $("Table"). Bind ("Click",function(e) {varTarget=E.target; $target=$ (target); if($target. Is ("TD") {$target. Text ('haha'); } })})</Script></Head><Body><Tablecellspacing= "1"> <TR> <TD>Cell</TD> <TD>Cell</TD> <TD>Cell</TD> <TD>Cell</TD> <TD>Cell</TD> </TR> <TR> <TD>Cell</TD> <TD>Cell</TD> <TD>Cell</TD> <TD>Cell</TD> <TD>Cell</TD> </TR></Table></Body></HTML>
The above code implements the same function, but the efficiency is greatly improved.
Summary: The so-called event delegate, is the event target itself does not handle the event, but the processing task entrusted to its parent element or ancestor element, even the root element.
The original address is: http://www.51texiao.cn/javascriptjiaocheng/2015/0428/328.html
The most original address is: http://www.softwhy.com/
What is the event delegate for jquery