drag & drop current element JS

巧了我就是萌 提交于 2021-02-11 12:31:29

问题


I have 2 divs and 2 paragraphs. Could someone say how could I put paragraph in current div? Example: 1paragraph --> 1div, but 1paragraph can't go to 2div. 2paragraph --> 2div, but 2paragraph can't go to 1div.

My Code:

var dragClass, dropClass;
function allowDrop(ev) {
  ev.preventDefault();
}

function drag(ev) {
  ev.dataTransfer.setData("Text", ev.target.id);
}

function drop(ev) {
  var data = ev.dataTransfer.getData("Text");
  ev.target.appendChild(document.getElementById(data));
  ev.preventDefault();
}
#div1 {
  width: 350px;
  height: 70px;
  padding: 10px;
  border: 1px solid #aaaaaa;
}
<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)" class="1element"></div>
<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)" class="2element"></div>
<br>
<p id="drag1" draggable="true" ondragstart="drag(event)" class="1element">This is a draggable paragraph. Drag this element into the rectangle.</p>
<p id="drag1" draggable="true" ondragstart="drag(event)" class="2element">This is a draggable paragraph. Drag this element into the rectangle.</p>

回答1:


Have you tried this?

Just set unique id of div and p tag.

var dragClass, dropClass;
function allowDrop(ev) {
  ev.preventDefault();
}

function drag(ev) {
  ev.dataTransfer.setData("Text", ev.target.id);
}

function drop(ev) {
  var data = ev.dataTransfer.getData("Text");
  ev.target.appendChild(document.getElementById(data));
  ev.preventDefault();
}
#div1 ,#div2{
  width: 350px;
  height: 70px;
  padding: 10px;
  border: 1px solid #aaaaaa;
}
   
<html>
<body>
<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)" class="1element"></div>
<div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)" class="2element"></div>
<br>
<p id="drag1" draggable="true" ondragstart="drag(event)" class="1element">This is a draggable paragraph. Drag this element into the rectangle.</p>
<p id="drag2" draggable="true" ondragstart="drag(event)" class="2element">This is a draggable paragraph. Drag this element into the rectangle.</p>
</body>
</html>



回答2:


The answer for this question is:

var dragClass, dropClass;


function allowDrop(ev) {
  dropClass = event.target.classList;
      if(`${dropClass}` === `${dragClass}`) {
        ev.preventDefault();
      } else {}
}

function drag(ev) {
   ev.dataTransfer.setData("Text", ev.target.id);
   dragClass = event.target.classList;
   
}

function drop(ev) {
  var data = ev.dataTransfer.getData("Text");
  ev.target.appendChild(document.getElementById(data));
  ev.preventDefault();
}
#div1 {
  width: 350px;
  height: 70px;
  padding: 10px;
  border: 1px solid #aaaaaa;
}
#div2 {
  width: 350px;
  height: 70px;
  padding: 10px;
  border: 1px solid #aaaaaa;
}
<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)" class="boat"></div>
<div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)" class="house"></div>
<br>
<p id="drag1" draggable="true" ondragstart="drag(event)" class="boat">This is a draggable paragraph. Drag this element into the rectangle.</p>
<p id="drag2" draggable="true" ondragstart="drag(event)" class="house">This is a draggable paragraph. Drag this element into the rectangle.</p>
<p id="demo"></p>


来源:https://stackoverflow.com/questions/65822050/drag-drop-current-element-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!