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
Release: 2023-09-08 19:45:02
forward
790 people have browsed it

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>
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template