简体   繁体   中英

HTML Drag and Drop API -

This is a simple math game.

My problem: I cannot drag the images in the middle and drag them to the left side. I did some research on the internet but could not find any results.

HTML:

<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" type="text/css" href="styleSheet.css" />
  <script src="myScript.js"></script>
</head>
<body>
  <!--İŞLEMLER-->
  <div id="islemler">
    <!--1.İŞLEM-->
    <div>
      <img src="images/question-mark.png" ondrop="dropHandler(event)" ondragover="dragoverHandler(event)">
      <img src="images/plus.png">
      <img src="images/question-mark.png" ondrop="dropHandler(event)" ondragover="dragoverHandler(event)">
      <img src="images/equal.png">
      <img src="images/eight.png">
    </div>


  </div>
  <!--TAŞINACAK RAKAMLAR-->
  <div id="tasinacakRakamlar">
    <img src="images/three.png" draggable="true" ondragstart="dragstartHandler(event)">
    <img src="images/five.png" draggable="true" ondragstart="dragstartHandler(event)">
  </div>
</body>
</html>

JS:

function dragstartHandler(e){
    e.dataTransfer.setData("text",e.target.id);
}

function dragoverHandler(e){
    e.preventDefault();
    //e.dataTransfer.dropEffect="copy";
}

function dropHandler(e){
    e.preventDefault();
    var myData = e.dataTransfer.getData("text");
    e.target.appendChild(document.getElementById(myData));
}

Well, your trying to getElementByID but your elements dosent have ID. put ID on your elements.

 function dragstartHandler(e){ e.dataTransfer.setData("text",e.target.id); } function dragoverHandler(e){ e.preventDefault(); //e.dataTransfer.dropEffect="copy"; } function dropHandler(e){ e.preventDefault(); var myData = e.dataTransfer.getData("text"); e.target.appendChild(document.getElementById(myData)); }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="styleSheet.css" /> <script src="myScript.js"></script> </head> <body> <!--İŞLEMLER--> <div id="islemler"> <!--1.İŞLEM--> <div> <img src="images/question-mark.png" ondrop="dropHandler(event)" ondragover="dragoverHandler(event)"> <img src="images/plus.png"> <img src="images/question-mark.png" ondrop="dropHandler(event)" ondragover="dragoverHandler(event)"> <img src="images/equal.png"> <img src="images/eight.png"> </div> </div> <!--TAŞINACAK RAKAMLAR--> <div id="tasinacakRakamlar"> <img src="images/three.png" draggable="true" id="ele1" ondragstart="dragstartHandler(event)"> <img src="images/five.png" draggable="true" id="ele2" ondragstart="dragstartHandler(event)"> </div> </body> </html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM