在HTML5中创建可拖动的段落
使用draggable 属性创建可拖动的段落。使用值true 和false启用 或禁用 拖动。
示例
您可以尝试运行以下代码来实现可拖动 元素和拖动元素-
<!DOCTYPE HTML> <html> <head> <style> .drag { float: left; width: 100px; height: 75px; border: 1px dashed #876587; margin: 15px; padding: 10px; } </style> </head> <body> <div class = "drag" ondrop = "drop(event)" ondragover = "dropNow(event)"> <p ondragstart = "dragStart(event)" ondrag = "draggingNow(event)" draggable = "true" id = "dragtarget">This is demo paragraph to be dragged!</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 draggingNow(event) { document.getElementById("box").innerHTML = "拖成功!"; } function dropNow(event) { event.preventDefault(); } function drop(event) { event.preventDefault(); var data = event.dataTransfer.getData("Text"); event.target.appendChild(document.getElementById(data)); document.getElementById("box").innerHTML = "元素成功删除!"; } </script> </body> </html>