ลากแล้วปล่อย (DnD) เป็นแนวคิดอินเทอร์เฟซผู้ใช้ที่มีประสิทธิภาพ ซึ่งทำให้ง่ายต่อการคัดลอก จัดลำดับ และลบรายการด้วยการคลิกเมาส์ ซึ่งช่วยให้ผู้ใช้สามารถคลิกปุ่มเมาส์ค้างไว้เหนือองค์ประกอบ ลากไปยังตำแหน่งอื่น และปล่อยปุ่มเมาส์เพื่อวางองค์ประกอบที่นั่น
สำหรับการลากและวาง:
<!DOCTYPE HTML>
<html>
<head>
<style>
#boxA, #boxB {float:left;padding:10px;margin:10px; -moz-user-select:none;}
#boxA { background-color: #6633FF; width:75px; height:75px; }
#boxB { background-color: #FF6699; width:150px; height:150px; }
</style>
<script>
function dragStart(ev) {
ev.dataTransfer.effectAllowed='move';
ev.dataTransfer.setData("Text", ev.target.getAttribute('id'));
ev.dataTransfer.setDragImage(ev.target,0,0);
return true;
}
</script>
</head>
<body>
<center>
<h2>Drag and drop HTML5 demo</h2>
<div>Try to drag the purple box around.</div>
<div id = "boxA" draggable = "true"
ondragstart = "return dragStart(ev)">
<p>Drag Me</p>
</div>
<div id = "boxB">Dustbin</div>
</center>
</body>
</html>