jQuery on()メソッド

❮jQueryイベントメソッド

クリックイベントを<p>要素にアタッチします。

$("p").on("click", function(){
  alert("The paragraph was clicked.");
});

定義と使用法

on()メソッドは、選択された要素と子要素に対して1つ以上のイベントハンドラーをアタッチします。

jQueryバージョン1.7以降、on()メソッドは、bind()、live()、およびdelegate()メソッドの新しい代替手段です。このメソッドはAPIに多くの一貫性をもたらします。このメソッドを使用すると、jQueryコードベースが簡素化されるため、このメソッドを使用することをお勧めします。

注: on()メソッドを使用してアタッチされたイベントハンドラーは、現在の要素と将来の要素(スクリプトによって作成された新しい要素など)の両方で機能します。

ヒント:イベントハンドラーを削除するには、off()メソッドを使用します。

ヒント:一度だけ実行されてからそれ自体を削除するイベントをアタッチするには、one()メソッドを使用します。


構文

$(selector).on(event,childSelector,data,function,map)

Parameter Description
event Required. Specifies one or more event(s) or namespaces to attach to the selected elements.

Multiple event values are separated by space. Must be a valid event
childSelector Optional. Specifies that the event handler should only be attached to the specified child elements (and not the selector itself, like the deprecated delegate() method).
data Optional. Specifies additional data to pass along to the function
function Required. Specifies the function to run when the event occurs
map Specifies an event map ({event:function, event:function, ...}) containing one or more event to attach to the selected elements, and functions to run when the events occur

自分で試してみてください-例


を添付する要素に複数のイベントを添付する方法。


を使用して、選択した要素に複数のイベントハンドラーをアタッチする方法。


にカスタマイズされた名前空間イベントをアタッチする方法。


にデータを渡す方法。


するon()メソッドがまだ作成されていない要素に対しても機能することを示します。


off()メソッドを使用してイベントハンドラーを削除する方法。


❮jQueryイベントメソッド