Drag and drop is part of the HTML5 standard.

Drag the RUNOOB.COM icon into the rectangle. Drag and drop is a common feature that grabs an object and then drags it to another location. In HTML5, drag and drop is part of the standard, and any element can be dragged and dropped. Internet Explorer 9, Firefox, Opera, Chrome, and Safari support dragging. Note: Safari 5.1.2 does not support dragging. The following example is a simple drag and drop example: It may seem a little complicated, but we can look at different parts of the drag-and-drop event separately. First, to make the element draggable, set the Then, specify what happens when the element is dragged. In the above example, the ondragstart property calls a function, drag (event), which specifies the data being dragged. By default, data / elements cannot be placed in other elements. If we need to set allow placement, we must block the default handling of the element. This is done by calling the ondragover event’s Occurs when the dragged data is placed In the above example Code interpretation: Call Pass through The data being dragged is the id (“drag1”) of the dragged element. Append the dragged element to the placement element 11.36.1. Drag and drop ¶
11.36.2. Browser support ¶
11.36.3. HTML5 drag and drop instance ¶
Example ¶
<!DOCTYPEHTML><html><head><metacharset="utf-8"><title>菜鸟教程(runoob.com)</title><styletype="text/css">#div1
{width:350px;height:70px;padding:10px;border:1px solid
#aaaaaa;}</style><script>function allowDrop(ev) { ev.preventDefault(); }
function drag(ev) { ev.dataTransfer.setData("Text",ev.target.id); }
function drop(ev) { ev.preventDefault(); var
data=ev.dataTransfer.getData("Text");
ev.target.appendChild(document.getElementById(data));
}</script></head><body><p>drag RUNOOB.COM
Picture to rectangular box:</p><divid="div1"ondrop="drop(event)"ondragover="allowDrop(event)"></div><br>
<imgloading="lazy"id="drag1"src="/images/logo.png"draggable="true"ondragstart="drag(event)"width="336"height="69"></body></html>
11.36.4. Set the element to be draggable ¶
draggable
property is set to
true
:<img draggable="true">
11.36.5. Drag what-
ondragstart
and
setData()
¶
dataTransfer.setData()
method to set the data type and value of the data being draggedfunction drag(ev)
{
ev.dataTransfer.setData("Text",ev.target.id);
}
Text
It’s a
DOMString
represents the type of drag data to add to the drag object. The value is the id (“drag1”) of the draggable element. 11.36.6. Where to put it-
ondragover
¶
ondragover
event specifies where to place the dragged data.
event.preventDefault()
methods:*event*.preventDefault()
11.36.7. Make placement-ondrop ¶
drop
Events.
ondrop
property calls a function
drop(event)
:function drop(ev)
{
ev.preventDefault();
var data=ev.dataTransfer.getData("Text");
ev.target.appendChild(document.getElementById(data));
}
preventDefault()
to avoid the browser’s default handling of data (the default behavior of the drop event is to open as a link)
dataTransfer.getData("Text")
method to get the data being dragged. The method will return in the
setData()
any data set to the same type in the.