官术网_书友最值得收藏!

Event delegation

Event delegation allows us to attach a single event listener to a parent element. This event will fire for all the descendants matching a selector even if these descendants will be created in the future (after the listener was bound to the element).

We discussed event bubbling earlier. Event delegation in jQuery works primarily due to event bubbling. Whenever an event occurs on a page, the event bubbles up from the element that it originated from, up to its parent, then up to the parent's parent, and so on, until it reaches the root element (window). Consider the following example:

<html>
  <body>
    <p id="container">
      <ul id="list">
        <li><a >Google</a></li>
        <li><a >Myntra</a></li>
        <li><a >Bing</a></li>
      </ul>
    </p>
  </body>
</html>

Now let's say that we want to perform some common action on any of the URL clicks. We can add an event handler to all the a elements in the list as follows:

$( "#list a" ).on( "click", function( event ) {
  console.log( $( this ).text() );
});

This works perfectly fine, but this code has a minor bug. What will happen if there is an additional URL added to the list as a result of some dynamic action? Let's say that we have an Add button that adds new URLs to this list. So, if the new list item is added with a new URL, the earlier event handler will not be attached to it. For example, if the following link is added to the list dynamically, clicking on it will not trigger the handler that we just added:

<li><a >Yahoo</a></li>

This is because such events are registered only when the on() method is called. In this case, as this new element did not exist when .on() was called, it does not get the event handler. With our understanding of event bubbling, we can visualize how the event will travel up the DOM tree. When any of the URLs are clicked on, the travel will be as follows:

a(click)->li->ul#list->p#container->body->html->root

We can create a delegated event as follows:

$( "#list" ).on( "click", "a", function( event ) {
  console.log( $( this ).text() );
});

We moved a from the original selector to the second parameter in the on() method. This second parameter of the on() method tells the handler to listen to this specific event and check whether the triggering element was the second parameter (the a in our case). As the second parameter matches, the handler function is executed. With this delegate event, we are attaching a single handler to the entire ul#list. This handler will listen to the click event triggered by any descendent of the ul element.

主站蜘蛛池模板: 化德县| 德阳市| 土默特右旗| 德江县| 洞头县| 宣化县| 满洲里市| 大渡口区| 清涧县| 蒙城县| 清镇市| 子洲县| 仁化县| 通江县| 延寿县| 年辖:市辖区| 太康县| 金堂县| 大竹县| 灵宝市| 祁东县| 三门峡市| 稻城县| 铜梁县| 宜宾县| 华容县| 卫辉市| 焉耆| 婺源县| 察雅县| 南川市| 凤城市| 平乡县| 临夏市| 保靖县| 宁波市| 沽源县| 铜川市| 慈利县| 辉南县| 清镇市|