无法在 div 上收听 dragend 事件

Unable to listen to dragend event on div

我有一个 div,我只想在其中收听 dragend 事件,但由于某种原因,它不起作用。当蓝色框落入矩形内但它什么也没给出时,我想控制台日志...

代码如下:

<!DOCTYPE html>
<html>
  <head>
    <style>
      #a {
        width: 350px;
        height: 70px;
        padding: 10px;
        border: 1px solid #aaaaaa; }
      #drag1 {
        background-color: blue;
        width: 100px;
        height: 70px;
      }
    </style>
  </head>

  <body>
    <p>Drag the blue box into the rectangle and wait for the console log:</p>

    <div id="a"></div>
    <br />
    <div id="drag1" draggable="true"></div>
  </body>

  <script>
    dropArea = document.getElementById("a");
    dragger = document.getElementById("drag1");
    dropArea.addEventListener("dragend", () => {
      console.log("Hello.....");
    });
  </script>
</html>

当您完成拖动时,dragend 事件从元素拖动开始

您必须将 dragend 事件放在拖动元素而不是放置区域

改变

dropArea.addEventListener("dragend", () => {

来自

dragger.addEventListener("dragend", () => {

    dropArea = document.getElementById("a");
    dragger = document.getElementById("drag1");
    dragger.addEventListener("dragend", () => {
      console.log("Here.....");
    });
<!DOCTYPE html>
<html>
  <head>
    <style>
      #a {
        width: 350px;
        height: 70px;
        padding: 10px;
        border: 1px solid #aaaaaa; }
      #drag1 {
        background-color: blue;
        width: 100px;
        height: 70px;
      }
    </style>
  </head>

  <body>
    <p>Drag the blue box into the rectangle and wait for the console log:</p>

    <div id="a"></div>
    <br />
    <div id="drag1" draggable="true"></div>
  </body>

</html>