最終更新:2015-01-14 (水) 06:27:27 (3383d)  

jQuery/イベント処理
Top / jQuery / イベント処理

処理の仕方

jQuery 1.9

jQuery.on (jQuery 1.7~)

jQuery.live (jQuery 1.3~)

  • .live( events, handler(eventObject) )
    $("a").live("click",function(e){
    //なんか処理
    });
     
  • Attach a handler to the event for all elements which match the current selector, now and in the future.
  • .live() and .delegate() check to see if the target element of a bubbled event matches certain criteria
  • ドキュメントルート(documentエレメント)にイベントハンドラを登録

jQuery.delegate (jQuery 1.4.2?~)

  • .delegate( selector, eventType, handler(eventObject) )
  • デリゲート
  • ドキュメントルートの代わりに$(...)で指定したエレメントに対してイベントハンドラを追加
  • live() との違いは、第1引数に jQueryセレクタ を指定できることです。 内部的では、live() が呼び出されています。
    $("#foo").delegate("a","click",function(e){
    なんか処理
    });
  • 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.

liveとdelegateの違い(下記は同じこと)

 $( '#container' ).delegate( '.link', 'click', handler );
 $( '.link', '#container' ).live( 'click', handler );

jQuery.bind (jQuery 1.0?~)

  • bindはその時点でjQueryオブジェクトに存在している要素のみが対象
  • .bind() creates a 1:1 association between element and handler

参考