Html5 拖放 Drag Drop

Html5 拖放 Drag Drop

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  <title>拖放(Drag 和 Drop)测试</title>  
  <style type="text/css">
        #div1, #div2 {float:left; width:240px; height:100px; margin:5px; padding:5px; border:1px solid #000000;}
    </style>
</head>  
<body>
  <h2>拖放(Drag 和 Drop)测试</h2>  
    <div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)">
  <img src="http://chanpinxue.cn/wp-content/uploads/2018/10/logo-2.png" draggable="true" ondragstart="drag(event)" id="drag1" width="240" height="100"></div>
    <div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
  <script type="text/javascript">
    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>
</body>
</html>

发表回复

您的电子邮箱地址不会被公开。