删除EventListener不起作用

删除EventListener不起作用

当我拖放文本时,什么也没有发生。但是,当我将事件更改为“ dragenter”或“ dragleave”时,相同的脚本有效。我有想念吗?



function handleDragDrop(e) {
  console.log("Something droped");
  dropStatus.innerHTML = "Something droped";
}

var dropZone = document.getElementById("dropZone");
var dropStatus = document.getElementById("dropStatus");


dropZone.addEventListener("drop", handleDragDrop);

.drop-zone {
  width: 300px;
  padding: 20px;
  border: 2px dashed #000;
}

<div id="dropZone" class="drop-zone">Drop Zone!</div>
<div id="dropStatus"></div>

<div class="" draggable="true">DRAG ME</div>

最佳答案

需要取消



function handleDragDrop(e) {
  console.log("Something droped");
  dropStatus.innerHTML = "Something droped";
}

var dropZone = document.getElementById("dropZone");
var dropStatus = document.getElementById("dropStatus");


dropZone.addEventListener("drop", handleDragDrop);
dropZone.addEventListener("dragover", function(e) {
  e.preventDefault();
  return false;
});

.drop-zone {
  width: 300px;
  padding: 20px;
  border: 2px dashed #000;
}

<div id="dropZone" class="drop-zone">Drop Zone!</div>
<div id="dropStatus"></div>

<div class="" draggable="true">DRAG ME</div>

关于javascript - 删除EventListener不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41863210/

10-13 09:13