&nb"/> &nb">

Home >Web Front-end >HTML Tutorial >Execute a script in HTML when an element is dragged to a valid drop target

Execute a script in HTML when an element is dragged to a valid drop target

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBforward
2023-09-08 19:45:02805browse

Execute a script in HTML when an element is dragged to a valid drop target

The ondragenter attribute fires when an element is dragged to a valid drop target in HTML.

Example

You can try running the following code to achieve ondragenter Properties -

<!DOCTYPE HTML>
<html>
   <head>
      <style type = "text/css">
         #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 type = "text/javascript">
         function dragStart(ev) {
            ev.dataTransfer.effectAllowed = &#39;move&#39;;
            ev.dataTransfer.setData("Text", ev.target.getAttribute(&#39;id&#39;));
            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>

The above is the detailed content of Execute a script in HTML when an element is dragged to a valid drop target. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete