多个拖放不起作用

时间:2017-07-14 21:18:36

标签: javascript html html5 drag-and-drop drag

到目前为止,我从一张图片开始,我可以将它拖到一个盒子里然后放下它,它工作正常。但是我有一个可以生成更多图像的按钮,当我将它们放在与原版相同的东西上时,它们就不会掉到那里。

我的代码:

 var myCount = 1;
    
      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));
      }
    
      function addImage() {
        var newImg = document.createElement("img");
        newImg.src = "apple.png";
        myCount += 1;
        var myString = "drag" + myCount.toString();
        newImg.id = myString;
        newImg.draggable = "true";
        newImg.ondragstart = "drag(event)";
        newImg.style = "width: 50px; height: 50px; padding-right: 4px;"
        var theDiv = document.getElementById('imgHolder');
        theDiv.appendChild(newImg);
      }
body {background-color: #ddd; font-family: arial, verdana, sans-serif;}
      #drop1 {width: 200px; height: 200px; border: 1px solid black; background-color: white;}
      #drag1 {width: 50px; height: 50px;}
      .drag {width: 50px; height: 50px;}
      <div id="drop1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
      <p> Drag the image below into the box above:</p>
      <div style="width: 50px; height: 50px; border:2px solid black; background-color: cyan; text-align: center;" onclick="addImage()">click me!</div>
      <div style="border: 2px solid black; height: 200px; width: 500px;" id="imgHolder">
        <img id="drag1" src="apple.png" draggable="true" ondragstart="drag(event)"/>
        </div>

任何帮助将不胜感激!我希望能够制作出我想要的新图像,并将它们放入盒子中!

2 个答案:

答案 0 :(得分:1)

根据https://www.w3schools.com/jsref/event_ondragstart.asp问题在newImg.ondragstart = "drag(event)";行中,格式应为newImg.ondragstart = function(){drag(event)};。它似乎有用......

<!DOCTYPE HTML>
<html>
<head>
  <title>Drag and Drop Test</title>
  <style>
  body {background-color: #ddd; font-family: arial, verdana, sans-serif;}
  #drop1 {width: 200px; height: 200px; border: 1px solid black; background-color: white;}
  #drag1 {width: 50px; height: 50px;}
  .drag {width: 50px; height: 50px;}
  </style>
  <script>

  var myCount = 1;

  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));
  }

  function addImage() {
    var newImg = document.createElement("img");
    newImg.src = "apple.png";
    myCount += 1;
    var myString = "drag" + myCount.toString();
    newImg.id = myString;
    newImg.draggable = "true";
    newImg.ondragstart = function(){drag(event)};
    //newImg.ondragstart = "drag(event)";
    newImg.style = "width: 50px; height: 50px; padding-right: 4px;"
    var theDiv = document.getElementById('imgHolder');
    theDiv.appendChild(newImg);
  }
  </script>
</head>

<body>
  <div id="drop1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
  <p> Drag the image below into the box above:</p>
  <div style="width: 50px; height: 50px; border:2px solid black; background-color: cyan; text-align: center;" onclick="addImage()">click me!</div>
  <div style="border: 2px solid black; height: 200px; width: 500px;" id="imgHolder">
    <img id="drag1" src="apple.png" draggable="true" ondragstart="drag(event)"/>
    </div>
  </div>
</body>
</html>

答案 1 :(得分:1)

你节点上的ongragstart函数需要指向像'newImg.ondragstart = drag;'这样的函数。使用函数的名称,而不是像'newImg.ondragstart =“drag(event)”;'

新代码如下所示:

<!DOCTYPE HTML>
<html>
<head>
  <title>Drag and Drop Test</title>
  <style>
  body {background-color: #ddd; font-family: arial, verdana, sans-serif;}
  #drop1 {width: 200px; height: 200px; border: 1px solid black; background-color: white;}
  #drag1 {width: 50px; height: 50px;}
  .drag {width: 50px; height: 50px;}
  </style>
  <script>

  var myCount = 1;

  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));
  }

  function addImage() {
    var newImg = document.createElement("img");
    newImg.src = "apple.png";
    myCount += 1;
    var myString = "drag" + myCount.toString();
    newImg.id = myString;
    newImg.draggable = "true";
    newImg.ondragstart = drag;
    newImg.style = "width: 50px; height: 50px; padding-right: 4px;"
    var theDiv = document.getElementById('imgHolder');
    theDiv.appendChild(newImg);
  }
  </script>
</head>

<body>
  <div id="drop1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
  <p> Drag the image below into the box above:</p>
  <div style="width: 50px; height: 50px; border:2px solid black; background-color: cyan; text-align: center;" onclick="addImage()">click me!</div>
  <div style="border: 2px solid black; height: 200px; width: 500px;" id="imgHolder">
    <img id="drag1" src="apple.png" draggable="true" ondragstart="drag(event)"/>
    </div>
  </div>
</body>
</html>