最終更新:2015-01-14 (水) 06:27:27 (3362d)
jQuery/イベント処理
処理の仕方
- jQuery.click
- jQuery.bind
- jQuery.on
- jQuery.live - jQuery 1.7からdeprecated(廃止予定)、jQuery 1.9でremoved(削除)
- jQuery.delegate - jQuery 1.7から非推奨
- http://qiita.com/nmta/items/310aa1cf385fa55129c1
jQuery 1.9
- jQuery.liveとjQuery.die?が削除
jQuery.on (jQuery 1.7~)
- Attach an event handler function for one or more events to the selected elements.
- jQuery.bind() jQuery.live() jQuery.delegate() がすべて jQuery.on() に統合
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