The code is attached:
<! DOCTYPE html>
<style type= "Text/css" >
#div1 {
width:198px;
height:66px;
padding:10px;
border:1px solid #aaaaaa;
}
</style>
<script type= "Text/javascript" >
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>
<body>
<p> please drag and drop the W3school picture into the rectangle:</p>
<div id= "Div1" ondrop= "Drop (event)" Ondragover= "AllowDrop (event)" ></div>
<br/>
//dragged picture
</body>
set elements to be dragged and dropped
First, to make an element draggable, set the Draggable property to true:
What to drag-ondragstart and SetData ()
Then, specify what happens when the element is dragged.
In the example above, the Ondragstart property invokes a function, drag (event), which specifies the data to be dragged.
The Datatransfer.setdata () method sets the data type and value of the dragged data: function drag (EV)
{
Ev.dataTransfer.setData ("Text", ev.target.id);
}
In this example, the data type is "Text" and the value is the ID of the draggable element ("Drag1"). where to put it-ondragover
The OnDragOver event specifies where to place the dragged data.
By default, data/elements cannot be placed in other elements. If you need to set allow placement, we must block the default handling of elements.
This is done by calling the Event.preventdefault () method of the OnDragOver event: Event.preventdefault ()
to place-OnDrop
The drop event occurs when the dragged data is dropped.
In the example above, the OnDrop property invokes a function, Drop (event): function drop (EV)
{
Ev.preventdefault ();
var data=ev.datatransfer.getdata ("Text");
Ev.target.appendChild (document.getElementById (data));
}