html5 拖放功能demo

index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    #div1 {width:auto;height:70px;padding:10px;border:1px solid #aaaaaa;}
    #container{
      width: 100%;
      height: auto;
      padding: 5px;
      border: 1px solid red;
    }

  </style>
  <script>
    function allowDrop(ev){
        ev.preventDefault();
    }
    
    function drag(ev){   
      console.log(ev.dataTransfer);
        ev.dataTransfer.setData("Text",ev.target.id);
        console.log(ev.dataTransfer);
    }
    
    function drop(ev){
        ev.preventDefault();
        var data=ev.dataTransfer.getData("Text");
        ev.target.appendChild(document.getElementById(data));
    }
    
  </script>
    
</head>
<body>

  <div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
  <img loading="lazy" id="drag1" src="./src/assets/logo.png" draggable="true" ondragstart="drag(event)" width="336" height="69">

  <div id="container" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
  <input type='text' id='dragEle' draggable="true" ondragstart="drag(event)"/>
  
</body>

</html>

  

版权声明:本文为fyc春原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/bky-1/p/16619818.html