【问题标题】:Multiple drag and drops not working多次拖放不起作用
【发布时间】:2017-07-14 21:18:35
【问题描述】:

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

我的代码:

 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>

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

【问题讨论】:

    标签: javascript html drag-and-drop drag


    【解决方案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>

    【讨论】:

      【解决方案2】:

      问题出在newImg.ondragstart = "drag(event)";这一行,根据https://www.w3schools.com/jsref/event_ondragstart.asp,格式应该是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>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-07-10
        • 2015-08-10
        • 2018-06-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多