What is the event delegate for jquery

Source: Internet
Author: User

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

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.