<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style> #dropbox { 300px; height: 300px; background: #ffffff; border:5px dashed #FF0000; text-align: center; line-height: 300px; } </style> </head> <body> <div id="dropbox"> 请把文件拖放到此区域 </div> <div id="div1"> <!--显示文件的信息--> </div> <script> //获得dom对象 var dropbox = document.getElementById('dropbox'); var div1 = document.getElementById('div1'); //拖动进入 dropbox.ondragenter = function(e) { console.log('dropbox dragenter'); e.preventDefault(); e.stopPropagation(); //区域高亮 this.style.background = 'yellow'; } //拖动离开 dropbox.ondragleave = function(e) { console.log('dropbox dragleave'); e.preventDefault(); e.stopPropagation(); //区域失去高亮 this.style.background = 'white'; } //拖动悬停 dropbox.ondragover = function(e) { console.log('dropbox dragover'); e.preventDefault(); e.stopPropagation(); } //释放元素 dropbox.ondrop = function(e) { console.log('dropbox drop'); e.preventDefault(); e.stopPropagation(); //从e.dataTransfer对象中获得文件列表对象 var files = e.dataTransfer.files; var html = []; //遍历文件列表 for (var i = 0;i < files.length;i ++) { //获得当前文件对象 var f = files[i]; //读取文件信息拼接字符串放到数组中 html.push('<p>' + f.name + ',' + (f.type||'未知类型') + ',' + f.size + '字节</p>'); } //在div1中显示文件信息 div1.innerHTML = html.join(''); } </script> </body> </html>