คลังความรู้ ระบบสารสนเทศ

Drag and Drop ใน HTML5

โดย สมโภชน์ กุลธารารมณ์

การเขียนคำสั่งในการ Drag and Drop รูปภาพด้วย HTML5

<!doctype html>

<html>

<head><title>test HTML5</title></head>

<body>
<script type=”text/javascript”>
function drag(ev){   ev.dataTransfer.setData(“Text”,ev.target.id);}

function drop(ev){var data=ev.dataTransfer.getData(“Text”);
ev.target.appendChild(document.getElementById(data));
ev.preventDefault();}

function allowdrop(ev)

{ev.preventDefault();}

</script>

<div id=”div1″ ondrop=”drop(event)” ondragover=”allowdrop(event)”style =”width:300px;height:300px;border:1px solid #cccccc;”></div>

<img id=”drag1″ src=”starbuck1.jpg” widht=”250″ height=”265″draggable=”true” ondragstart=”drag(event)”/>

</body>
</html>