jquery Dynamic bound event handler Bind/on/delegate

Source: Internet
Author: User

1. The Bind method binds an event handler that is not applied to a new element that is later added to the DOM. For example, after you bind an event to a page element with BIND, a new DOM element is added with the same elements as the previously bound event, but these events are not reflected on the new DOM element.

Such as:

$ (document). Ready (function() {      $ ("img"). Bind ({          mouseenter:function () {                   $ (this). CSS ("border", "thick solid Red");           },           mouseout:function  () {                  $ (this). CSS ("border", "thick solid green");           }         );       $ ("#row"). Append (");      });

The newly inserted IMG does not have a binding event at this time.

2. The on method binds an event and does not directly bind the event handler to the target element. In fact, it is binding an event handler on the document object that checks to see if the element that triggered the event matches the selector when triggered. Once the event matches the element successfully, the bound event handler is called. In fact, the on method is an effort to bind the event handler function to the new element.

$ (document). Ready (function() {      $ (document). On ({          mouseenter:function() {                   $ (this). CSS ("border", "thick solid Red")           ,           mouseout:function  () {                  $ (this). CSS ("border", "thick solid green");         }, "img"
$ ("#row"). Append ("
});

However, there may be a problem with the on method binding event handler, which we need to propagate to the document element before the processing function executes, and delegate more directly and efficiently than the on method.

3, delegate method binding event, allows us to specify any element on the page as an element of the listener event, binding only one event handler, binding speed with respect to bind and on is calculated faster.

$ (document). Ready (function() {      $ (
      
MouseEnter:function
$ ( This

Mouseout:function
$ ( This



});

jquery Dynamic bound event handler Bind/on/delegate

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.