问题
i have menu from where can drag elements and drop into another div, it works. But how i can do the replacement of elements??
function allowDrop(ev) {
ev.preventDefault();
}
function drag(ev) {
ev.dataTransfer.setData("text", ev.target.id);
}
function OnDragEnter (){
}
function drop(ev) {
ev.preventDefault();
var data = ev.dataTransfer.getData("text");
var nodeCopy = document.getElementById(data).cloneNode(true);
nodeCopy.id = "newId";
ev.target.appendChild(nodeCopy);
}
回答1:
Try this:
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.removeChild(ev.target.childNodes[0]);
ev.target.appendChild(document.getElementById(data));
}
来源:https://stackoverflow.com/questions/27089260/drag-and-drop-replace-in-html-5