0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

jQueryの .click() と .on("click") の違いを理解する

Posted at
jQuery("セレクタ").on("click", function() {
    // クリック時の処理
});

.on("click", function() { })

特徴

  1. 後から追加される要素(動的要素)にも対応する。(イベントデリゲーションが使える)

    ※イベントデリゲーションとは、親要素にイベントをバインドし、そのイベントが子要素に伝播(バブリング)する仕組みを利用することで、後から追加される要素にもイベントを適用できる手法。

  2. 複数のイベントをまとめてバインドできる(例:on("mouseenter mouseleave", function() {}))

jQuery("セレクタ").click(function() {
    // クリック時の処理
});

.click(function() { })

特徴

  1. 後から追加される要素には対応できない。
  2. 記述が.on("click", function() {})より短くなるが、動的要素には適用できないため、基本的には.on()の使用が推奨される。

まとめ

.on("click", function(){}) の方が後から追加した要素にも対応できるので柔軟性が高い。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?