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~✪

  • 相关阅读:
    关于排序--sort()和qsort()使用
    UVA--147 Dollars(完全背包)
    UVA--674 Coin Change(完全背包)
    HDU--1203 I NEED A OFFER!(01背包)
    编程中关于无穷大的设定技巧
    HDU--2126 Buy the souvenirs(二维01背包)
    HDU--2639 Bone Collector II(01背包)
    MySQL中的group_concat函数
    Redis监控
    JAVA中的代理模式
  • 原文地址:https://www.cnblogs.com/younglaker/p/3349398.html
Copyright © 2011-2022 走看看