I've created a simple drag and drop setup, 2 divs that allow a user to drag a child div between the two. It works fine unless a 'child div' is dropped directly inside another. I've been trying to wrap my head around it for hours and there must be a simple solution i am missing.
You can see a (not quite) working demo here
https://preview.c9.io/teemoash/fantasyleague/gamething/pete.html?_c9_id=livepreview4&_c9_host=https%3A%2F%2Fide.c9.io
any help is greatly appreciated
Thanks!
The html is very simple. ( note that i have tried returning false on onDrop and onDragOver events)
<div id = "squad" ondrop="drop(event)" ondragover="allowDrop(event)">
<h1>SQUAD</h1>
<div id = "jeff" class = "champion" draggable = "true" ondragstart="drag(event)" ondrop = "return false" ondragover="return false">
<h1>Jeff</h1>
<div class = "attributes">
<div class = "number kills"><span> 4</span><p>Kills</p></div>
<div class = "number deaths"><span>2 </span><p>Deaths</p></div>
<div class = "number GPM"><span> 12</span><p>GPM</p></div>
</div>
</div>
<div id = "Geoff" class = "champion" draggable = "true" ondragstart="drag(event)" ondrop = "return false" ondragover="return false">
<h1>Geoff</h1>
<div class = "attributes">
<div class = "number kills"><span> 7</span><p>Kills</p></div>
<div class = "number deaths"><span>0 </span><p>Deaths</p></div>
<div class = "number GPM"><span> 14</span><p>GPM</p></div>
</div>
</div>
<div id = "jeph" class = "champion" draggable = "true" ondragstart="drag(event)" ondrop = "return false" ondragover="return false">
<h1>Jeph</h1>
<div class = "attributes">
<div class = "number kills"><span> 1</span><p>Kills</p></div>
<div class = "number deaths"><span>9 </span><p>Deaths</p></div>
<div class = "number GPM"><span> 24</span><p>GPM</p></div>
</div>
</div>
</div> <!-- end of squad div-->
<div id = "myTeam" ondrop="drop(event)" ondragover="allowDrop(event)">
<h1>My Team</h1>
</div>
<div id = "scores">
<h1>My Team Scores</h1>
</div>
and the js looks like this;
<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>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…