简单教程
提交运行
代码编辑器:
<!DOCTYPE html> <meta charset="utf-8"> <style type="text/css"> #div1, #div2 {float:left; width:88px; height:52px; margin:10px;padding:10px;border:1px solid #aaaaaa;} </style> <script> function allowDrop(ev) { ev.preventDefault(); } function drag(ev) { ev.dataTransfer.setData("Text",ev.target.id); } function drop(ev) { ev.preventDefault(); var data=ev.dataTransfer.getData("Text"); ev.target.appendChild(document.getElementById(data)); } </script> <div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"> <img src="/static/i/img1.jpg" draggable="true" ondragstart="drag(event)" id="drag1" width="88"></div> <div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
运行结果: