LoginSignup
1
1

More than 5 years have passed since last update.

【 javascript / jQuery 】event.targetとevent.currentTerget

Last updated at Posted at 2018-05-27

よくあるネタですが備忘録として。。。

$('li').on('click', $.proxy(function(e) {

  var $target = $(e.target);

  this.xxxx($(this), $target);

・・・

},this));

上記みたいな書き方をしてしまうと
liの中の他要素をクリックした時、クリックした要素、つまりイベントが発火した要素が
$targetとして保存されてしまったりする。

これは単純にイベントが発生した要素がevent.targetで取得できるため。

常にliのようにイベントが通過した要素を取得したい場合は
イベントリスナを登録した要素を取得するcurrentTargetを利用する。

1
1
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
1
1