该ondrop事件触发时,拖动的元素被丢弃的目标。您可以尝试运行以下代码以了解如何在JavaScript中实现ondrop事件-
<!DOCTYPE HTML> <html> <head> <style> .drag { float: left; width: 100px; height: 35px; border: 2px dashed #876587; margin: 15px; padding: 10px; } </style> </head> <body> <div class="drag" ondrop="drop(event)" ondragover="dropNow(event)"> <p ondragstart="dragStart(event)" ondragend="dragEnd(event)" draggable="true" id="dragtarget">Drag!</p> </div> <div class="drag" ondrop="drop(event)" ondragover="dropNow(event)"></div> <div id="box"></div> <p>Drag the left box to the right or drag the right box to the left.</p> <script> function dragStart(event) { event.dataTransfer.setData("Text", event.target.id); } function dropNow(event) { event.preventDefault(); } function dragEnd(event) { document.getElementById("box").innerHTML = "拖完了!"; } function drop(event) { event.preventDefault(); var data = event.dataTransfer.getData("Text"); event.target.appendChild(document.getElementById(data)); document.getElementById("box").innerHTML = "元素成功删除!"; } </script> </body> </html>