zoukankan      html  css  js  c++  java
  • 【JQuery】Differences among bind(), live(), delegare, on() and one()

    1.Basic usage and description

    1.1.bind( eventType [, eventData ], handler(eventObject) )

    http://api.jquery.com/bind/

    Description: Attach a handler to an event for the elements.

    Unbind function:  unblnd()

     

    1.2.live( events, handler(eventObject) ) 

     http://api.jquery.com/live/  Remoed from 1.9 version

     Description: Attach an event handler for all elements which match the current selector, now and in the future

    Unbind function:  die()

     

    1. 3.delegate( selector, eventType, handler(eventObject) )

     http://api.jquery.com/delegate/

     Description: Attach a handler to one or more events for all elements that match the selector, now or in the future, based on a specific set of root elements.

    Unbind function:  undelegate()

     

     1.4.on( events [, selector ] [, data ], handler(eventObject) )

     http://api.jquery.com/on/

     Description: Attach an event handler function for one or more events to the selected elements.

    Unbind function:  off()

     

    1.5.one( events [, data ], handler(eventObject) )

     http://api.jquery.com/one/

     Description: Attach a handler to an event for the elements. The handler is executed at most once per element.

    Unbind function:  off()

     

     2.Diffences 

    2.1

    bind() only can bind the exist elements. live() and  delegate() can bind the elements create in the feature. They find the elements though bubble method .

    2.2

    delegate() and on() can bind the father elments to bind the children.

     $( "table" ).delegate( "td", "click", function() {

      $( this ).toggleClass( "chosen" );
    });

    2.4

    on() has the same effect but diffenrent usage, be careful about the arguments

    $( "table" ).on( "click", "td", function() {

      $( this ).toggleClass( "chosen" );

    });

     2.3.

    one() only excute for once.

    2.5.

    In the newest version, all the other bind function is based on on(). And all unbind function are based on off().

    Here are some snippets in jquery 1.10.1 (live() has already moved from 1.9)

      

    ≡≡≡≡≡≡≡≡≡≡≡≡★ From Laker's blog ★≡≡≡≡≡≡≡≡≡≡≡≡

    ¤ ╭⌒╮☀Coding and changing~☀ ╭╭ ⌒╮

    ✪Fighting and insisting~✪

  • 相关阅读:
    并行取数提升报表性能
    报表选型中那些想不到的坑
    原来报表可以做这么多动态交互效果
    多折线堆叠图如何制作?
    SSIS文档导入DB中文乱码
    Linux-系统日志
    linux-用户和组的管理
    LInux-用户和用户组
    dotcore发布到IIS
    vue发布
  • 原文地址:https://www.cnblogs.com/younglaker/p/3349398.html
Copyright © 2011-2022 走看看