清静经 2019-05-17 13:24 采纳率: 0%
浏览 1184

html通过draggable如何实现将拖放后位置原有的内容删除?

模拟车在车库中换车位的过程,将车从一个车位拖到其余的空车位上,然后让空车位原有的图片消失,显示拖动的车的图片。目前只实现了一部分,如下:图片说明
代码如下:

    <script>
        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));
        }
    </script>
    <style>
        #tal1 td{
            border: 1px solid black;
        }
    </style>
    <table width="300px" height="300px">
        <tr>
            <td ondrop="drop(event)" ondragover="allowDrop(event)"><img draggable="true" ondragstart="drag(event)" id="im1" src="img/1.png"/></td>
            <td ondrop="drop(event)" ondragover="allowDrop(event)"><img draggable="true" ondragstart="drag(event)" id="im2" src="img/1.png"/></td>
            <td ondrop="drop(event)" ondragover="allowDrop(event)"><img id="im3" src="img/e.png"/></td>
        </tr>
        <tr>
            <td ondrop="drop(event)" ondragover="allowDrop(event)"><img draggable="true" ondragstart="drag(event)" id="im4" src="img/1.png"/></td>
            <td ondrop="drop(event)" ondragover="allowDrop(event)"><img id="im5" src="img/e.png"/></td>
            <td ondrop="drop(event)" ondragover="allowDrop(event)"><img id="im6" src="img/e.png"/></td>
        </tr>
        <tr>
            <td ondrop="drop(event)" ondragover="allowDrop(event)"><img id="im7" src="img/e.png"/></td>
            <td ondrop="drop(event)" ondragover="allowDrop(event)"><img id="im8" src="img/e.png"/></td>
            <td ondrop="drop(event)" ondragover="allowDrop(event)"><img id="im9" src="img/e.png"/></td>
        </tr>
    </table>
  • 写回答

1条回答 默认 最新

  • mannymu 2019-05-17 19:57
    关注
      <script>
            function allowDrop(ev)
            {
                ev.preventDefault();
            }
            function drag(ev)
            {   var oid = ev.target.id
                if(!oid){
                    oid = "dragging" + (Date.now()+ Math.floor(Math.random()*1000 + 100)); //生成随机的 id
                    ev.target.setAttribute("id",oid);
                }
                ev.dataTransfer.setData("Text",oid);
            }
            function drop(ev)
            {
                ev.preventDefault();
                var data=ev.dataTransfer.getData("Text");
                var dom = document.getElementById(data);
                var parent = dom.parentNode;
                parent.className = ""; //把 drop 类名去掉,显示出空位
    
                ev.target.className = "empty"; //加上 drop 类名,隐藏空位
                ev.target.appendChild(dom);
            }
        </script>
        <style>
            #tal1 td{
                border: 1px solid black;
            }
             #tal1 td.drop .empty{
                 display: none;
             }
        </style>
        <table width="300px" height="300px">
            <tr>
                <td ondrop="drop(event)" ondragover="allowDrop(event)" class="drop">
                    <img draggable="true" ondragstart="drag(event)"    src="img/1.png"/>
                    <img  src="img/e.png" class="empty" />
                </td>
               <td ondrop="drop(event)" ondragover="allowDrop(event)">
                <img draggable="true" ondragstart="drag(event)"    src="img/1.png"/>
                <img  src="img/e.png" class="empty" />
               </td>
                <td ondrop="drop(event)" ondragover="allowDrop(event)">
                    <img  src="img/e.png" class="empty" />
                </td>
            </tr>
            <tr>
                <td ondrop="drop(event)" ondragover="allowDrop(event)">
                    <img  src="img/e.png" class="empty" />
                </td>
               <td ondrop="drop(event)" ondragover="allowDrop(event)">
                <img  src="img/e.png" class="empty" />
               </td>
                <td ondrop="drop(event)" ondragover="allowDrop(event)">
                    <img  src="img/e.png" class="empty" />
                </td>
            </tr>
          <tr>
              <td ondrop="drop(event)" ondragover="allowDrop(event)">
                <img  src="img/e.png" class="empty" />
            </td>
             <td ondrop="drop(event)" ondragover="allowDrop(event)">
                <img  src="img/e.png" class="empty" />
             </td>
              <td ondrop="drop(event)" ondragover="allowDrop(event)" class="drop">
                <img draggable="true" ondragstart="drag(event)"   src="img/1.png"/>
                <img  src="img/e.png" class="empty" />
              </td>
          </tr>
        </table>
    

    大概就是这样了

    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题