简体   繁体   中英

How can I replace the content of a div when I drag and drop

How can I drag and replace the content when I drop; in this example: http://jsbin.com/uvihox/5/edit ?

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));
}

try this:

function drop(ev){
    ev.preventDefault();
    var data=ev.dataTransfer.getData("Text");
    var s=document.getElementById(data);
    ev.target.appendChild(s);
    ev.target.src=s.src;
}

update your drop function like this. basically, you need to update the src of dropped img. It works for now.

updated fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM