我想从我将图像放入div的div中获取div id。例如,我将它放入div 1中我希望打印文本1,当我将图像放在div 2中时,它是相同的。
function dragStart(event) {
event.dataTransfer.setData("Text", event.target.id);
}
function allowDrop(event) {
event.preventDefault();
}
function drop(event) {
event.preventDefault();
var data = event.dataTransfer.getData("Text");
event.target.appendChild(document.getElementById(data));
document.getElementById("demo").innerHTML = event.dataTransfer.setData('Text/html', event.target.id);
}
.droptarget {
float: left;
width: 100px;
height: 35px;
margin: 15px;
padding: 10px;
border: 1px solid #aaaaaa;
}
<div id="1" class="droptarget" ondrop="drop(event)" ondragover="allowDrop(event)">
<img src="img/chip.png" width="30" height="30" ondragstart="dragStart(event)" draggable="true" id="dragtarget">
</div>
<div id="2" class="droptarget" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<p id="demo"></p>
答案 0 :(得分:0)
你刚刚替换
document.getElementById("demo").innerHTML = ent.dataTransfer.setData('Text/html', event.target.id);
到
document.getElementById("demo").innerHTML = event.target.id;