14
13

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 5 years have passed since last update.

Firefox のイベントリスナで drop イベントを受け取るには dragover をpreventDefault する

Last updated at Posted at 2016-09-16

Chrome, Safari は el.addEventListener("drop", () => {}) だけで動くが、Firefox は動かなくてハマった。

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <div class="container" style="height: 200px; width: 200px; background-color: red;"></div>
    <script>
      const container = document.querySelector(".container");
      container.addEventListener("dragover", (ev) => {
        ev.preventDefault(); // これがないと drop が発火しない
      });
      container.addEventListener("dragenter", (ev) => {
        ev.preventDefault(); // 一部のブラウザではこれも必要、というのを見かけたが、どのブラウザか不明
      });
      container.addEventListener("drop", (ev) => {
        ev.preventDefault();
        console.log("drop");
      });
    </script>
  </body>
</html>

とのこと

14
13
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
14
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?