我创建了一个简单的拖放设置,2个div,允许用户在两者之间拖动子div。除非将'child div'直接放入另一个内,否则它可以正常工作。我一直试图绕着它缠绕几个小时,必须有一个我想念的简单解决方案。
你可以在这里看到(不完全)工作演示 https://preview.c9.io/teemoash/fantasyleague/gamething/pete.html?_c9_id=livepreview4&_c9_host=https%3A%2F%2Fide.c9.io
非常感谢任何帮助
谢谢!
html非常简单。 (请注意,我已尝试在onDrop和onDragOver事件上返回false)
<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>
并且js看起来像这样;
<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>
答案 0 :(得分:33)
问题在于drop与事件有关,但你拥有它的方式应该只是绑定到这样的元素:
function drop(ev, el) {
ev.preventDefault();
var data = ev.dataTransfer.getData("text");
el.appendChild(document.getElementById(data));
}
然后在您的两个drop(event)
活动中将drop(event, this)
更改为ondrop
。看看全页模式下的片段,看它是否有效。
function allowDrop(ev) {
ev.preventDefault();
}
function drag(ev) {
ev.dataTransfer.setData("text", ev.target.id);
}
function drop(ev, el) {
ev.preventDefault();
var data = ev.dataTransfer.getData("text");
el.appendChild(document.getElementById(data));
}
&#13;
* {
margin: 0;
box-sizing: border-box;
}
#squad {
width: 40vw;
height: 90vh;
overflow-y: scroll;
border-radius: 10px;
margin-left: 5vw;
margin-top: 5vh;
background-color: red;
float: left;
}
h1 {
text-align: center;
}
#myTeam {
float: left;
height: 60vh;
width: 40vw;
border-radius: 10px;
background-color: red;
margin-left: 10vw;
margin-top: 5vh;
}
#scores {
width: 40vw;
height: 25vh;
margin-top: 5vh;
margin-left: 10vw;
background-color: red;
border-radius: 10px;
float: left;
}
.champion {
width: 90%;
height: 15vh;
margin: 1%;
padding: 2%;
border: 1px black solid;
background-color: white;
border-radius: 10px;
overflow: hidden;
}
.champion h1 {
float: left;
}
.attributes {
margin-left: 10%;
float: left;
}
.number {
float: left;
text-align: center;
margin-left: 10px;
}
#div1,
#div2 {
width: 90%;
height: 15vh;
margin: 1%;
border: 1px black solid;
border-radius: 10px;
}
&#13;
<div id="squad" ondrop="drop(event, this)" 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, this)" ondragover="allowDrop(event)">
<h1>My Team</h1>
</div>
<div id="scores">
<h1>My Team Scores</h1>
</div>
&#13;