0

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

1 回答 1

1

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

于 2013-03-21T09:21:49.437 回答