对于整页的拖放文件,请尝试以下代码:
var myDrop = document.getElementById('dropZone'); function displayDropZone() { myDrop.style.visibility = "visible"; } function hideDropZone() { myDrop.style.visibility = "hidden"; } function allowDrag(ev) { if (true) { ev.dataTransfer.dropEffect = 'copy'; ev.preventDefault(); } } function handleDrop(ev) { ev.preventDefault(); hideDropZone(); alert('This is Drop!'); } window.addEventListener('dragenter', function(ev) { displayDropZone(); }); myDrop.addEventListener('dragenter', allowDrag); myDrop.addEventListener('dragover', allowDrag); myDrop.addEventListener('dragleave', function(e) { hideDropZone(); }); myDrop.addEventListener('drop', handleDrop);