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?

More than 3 years have passed since last update.

イベントリスナーを削除する方法【javascript】

Posted at

設定したイベントを削除するには

要素.removeEventListener('イベント名', 関数名);

上記のように記述することで設定したイベントリスナーを削除できる。

removeEventListenerが反映されない

useCaptureの値が違う時は、removeEventListenerが効かないとのこと。
参考にさせて頂いたサイト:https://pisuke-code.com/js-not-work-removeeventlistener/
   
useCaptureとは、removeEventListenerの第三引数にあたる値のこと。
書き方としては、trueかfalseを記述するそうですが、何も書かなかった場合はデフォルトでfalseが設定されるとのことでした。

これがtrueの時は、windowからイベントが発生した要素に行きつくまでのイベント通知が有効になり、falseの時はイベントが発生した要素から、windowに行きつくまでのイベント通知が有効になるそう。

参考にさせて頂いたサイト:https://note.affi-sapo-sv.com/js-addeventlistener-usecapture.php#title2

useCaptureのことを調べてみましたが、理解ができないところが多かったので深堀りをして改めて補足させて頂くかもしれません。

0
0
1

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?