有时候是浏览器的原因,并不是代码的原因哦,大家可以换多个浏览器尝试。
代码如下:WebStrom工具
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .album{ border: 3px dashed #ccc;float: left;margin: 10px;min-height: 100px;padding: 10px;width: 220px; } </style> <script> function allowDrop(ev){ ev.preventDefault();}function drag(ev){ ev.dataTransfer.setData("imgId",ev.target.id); } function drop(ev){ ev.preventDefault(); var data=ev.dataTransfer.getData("imgId"); ev.target.appendChild(document.getElementById(data)); } </script> <body> <div id="info"> <h2>温馨提示:可将照片直接拖到垃圾箱中</h2> </div> <div id="album" class="album" οndragstart="drag(event)"> <h2>相册</h2> <img draggable="true" id="img1" src="KD1.jpg" width="100px" height="100px" /> <img draggable="true" id="img2" src="KD.jpg" width="100px" height="100px" /> <img draggable="true" id="img3" src="KD1.jpg" width="100px" height="100px" /> <img draggable="true" id="img4" src="KD.jpg" width="100px" height="100px" /> <img draggable="true" id="img5" src="KD1.jpg" width="100px" height="100px" /> <img draggable="true" id="img6" src="KD.jpg" width="100px" height="100px" /> </div> <div id="trash" class="album" οndrοp="drop(event)" οndragοver="allowDrop(event)"> <h2>垃圾箱</h2> </div> </body> </html>