简体   繁体   English

当我将图片放入区域时,为什么 addEventListener 不起作用?

[英]Why isn't the addEventListener working when I drop picture in zone?

I'm making "Drag and Drop File Upload".我正在制作“拖放文件上传”。 But Now I have a problem.但是现在我有一个问题。

As you see that,When I drag something on "drop-zone", The border-style become solid.正如你所看到的,当我在“drop-zone”上拖动一些东西时,边框样式变成了实体。

Then When I leave my mouse from "drop-zone", The border-style become dashed.然后,当我将鼠标从“拖放区”离开时,边框样式变为虚线。

The problem is when I drop something on "drop-zone", It doesn't come back to dashed.问题是当我在“drop-zone”上放置一些东西时,它不会回到虚线。

What's my problem with the code?我的代码有什么问题?

 document.querySelectorAll(".drop-zone__input").forEach(inputElement => { let dropZoneElement = inputElement.closest(".drop-zone"); dropZoneElement.addEventListener("dragover", e => { dropZoneElement.classList.add("drop-zone__over"); }); ["dragleave", "dragend"].forEach(type => { dropZoneElement.addEventListener(type, e => { dropZoneElement.classList.remove("drop-zone__over"); }); }); dropZoneElement.addEventListener("drop", e => { dropZoneElement.classList.remove("drop-zone__over"); }); });
 .drop-zone { border: 5px dashed plum; display: flex; justify-content: center; align-items: center; text-align: center; width: 250px; height: 250px; font-size: 30px; font-weight: bold; padding: 30px; border-radius: 10px; cursor: pointer; }.drop-zone__over { border-style: solid; }.drop-zone__thumb { width: 100%; height: 100%; background-color: rgb(207, 207, 207); border-radius: 10px; position: relative; overflow: hidden; }.drop-zone__thumb::after { content: attr(data-label); position: absolute; bottom: 0; left: 0; width: 100%; background-color: rgb(114, 113, 113); padding: 5px 0; }.drop-zone__input { display: none; }
 <div class="drop-zone"> <span class="drop-zone__prompt">Click here or Drag a picture</span> <.-- <div class="drop-zone__thumb" data-label="tesdfsdfsdfsdfsdfsdfsxt.txt"></div> --> <input type="file" name="myFile" class="drop-zone__input"> </div>

does adding e.preventDefault() fix it in your original code?添加e.preventDefault()会在您的原始代码中修复它吗?

 document.querySelectorAll(".drop-zone__input").forEach(inputElement => { let dropZoneElement = inputElement.closest(".drop-zone"); dropZoneElement.addEventListener("dragover", e => { e.preventDefault(); // HERE dropZoneElement.classList.add("drop-zone__over"); }); ["dragleave", "dragend"].forEach(type => { dropZoneElement.addEventListener(type, e => { dropZoneElement.classList.remove("drop-zone__over"); }); }); dropZoneElement.addEventListener("drop", e => { e.preventDefault(); // HERE dropZoneElement.classList.remove("drop-zone__over"); }); });
 .drop-zone { border: 5px dashed plum; display: flex; justify-content: center; align-items: center; text-align: center; width: 250px; height: 250px; font-size: 30px; font-weight: bold; padding: 30px; border-radius: 10px; cursor: pointer; }.drop-zone__over { border-style: solid; }.drop-zone__thumb { width: 100%; height: 100%; background-color: rgb(207, 207, 207); border-radius: 10px; position: relative; overflow: hidden; }.drop-zone__thumb::after { content: attr(data-label); position: absolute; bottom: 0; left: 0; width: 100%; background-color: rgb(114, 113, 113); padding: 5px 0; }.drop-zone__input { display: none; }
 <div class="drop-zone"> <span class="drop-zone__prompt">Click here or Drag a picture</span> <.-- <div class="drop-zone__thumb" data-label="tesdfsdfsdfsdfsdfsdfsxt.txt"></div> --> <input type="file" name="myFile" class="drop-zone__input"> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM