html5 拖放功能
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>