LoginSignup
11
11

More than 5 years have passed since last update.

jQuery Event から DOM Event を取る

Posted at

結論

event.originalEventで取れる。

解説

jQueryのイベント便利ですよね。
一気に複数イベント登録できたり.onceや名前空間など様々な機能があって便利です。

しかし、ドラッグアンドドロップイベントのdataTransferや、コピーアンドペーストのclipboardDataなどはjQueryのeventでは直接使えません。

$("#editor").on("paste", function(event){
  alert(event.clipboardData.getData("text/plain") + "をペーストするよ!");
});
// => エラー!!

このとき関数に渡される引数はjQueryの独自のイベントオブジェクトなのです。
ブラウザ間で差異がないようよしなにやってくれてるわけですが、addEventListenerなどで渡されるオリジナルのイベントオブジェクトが欲しいです。

実は、このオリジナルのイベントオブジェクトは、jQueryのイベントオブジェクトのoriginalEventプロパティで参照できるようになっています。

なので、上記のコードは、

$("#editor").on("paste", function(event){
  alert(event.originalEvent.clipboardData.getData("text/plain") + "をペーストするよ!");
});

のように直すと動きます。

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