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 1 year has passed since last update.

[jQuery]ドラッグアンドドロップでe.dataTransfer undefinedになる

Last updated at Posted at 2022-03-28

jQueryでドラッグアンドドロップ機能を作っている際、
e.dataTransfer.filesでドロップされたファイルが取得できない問題に出会った。

先に結論

e.originalEvent.dataTransfer.filesでアクセスすればいける。
jQueryのeventは独自のeventらしい。。わかりづらい
originalEventが生のjsのイベントのようです。

参考:jQuery Event から DOM Event を取る

公式:https://api.jquery.com/category/events/event-object/

解決

元のコード

元のコード.js
drag_area.on('drop', function (e) {
    e.preventDefault();
    // ...
    this.input.files = e.dataTransfer.files;
}

修正コード

修正コード.js
drag_area.on('drop', function (e) {
    e.preventDefault();
    // 変更
    this.input.files = e.originalEvent.dataTransfer.files;
}
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?