2017-01-09 121 views
0

我想從div中刪除圖像的div id。恩我拖放到1區我希望文本1要打印和相同的,當我在格下降的圖像2.拖放事件javascript

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>

+3

'document.getElementById(「demo」)。innerHTML = event.target.id;'' – Andreas

回答

0

您只需更換

document.getElementById("demo").innerHTML = ent.dataTransfer.setData('Text/html', event.target.id); 

document.getElementById("demo").innerHTML = event.target.id;