admin管理员组文章数量:1277901
I tried swapping two elements inside div1 and div2 tag but only the first replace function executes properly the other element is not swapped
either one of the div is getting swapped at once but when i try to swap both at same time it doesn't happen please help me out *
<head> <style type="text/css"> #div1 { width:350px; height:70px; padding:10px; border:1px solid #aaaaaa; } #div2 { width:350px; height:70px; padding:10px; border:1px solid #aaaaaa; } </style> <script> function allowDrop(ev) { ev.preventDefault(); } function drag(ev) { var child = ev.target; var parents = child.parentNode; ev.dataTransfer.setData("child1", child.id); ev.dataTransfer.setData("parent1", parents.id); } function drop(ev) { ev.preventDefault(); var childid1 = ev.dataTransfer.getData("child1"); var parentid1 = ev.dataTransfer.getData("parent1"); var parent1 = document.getElementById(parentid1); var c = ev.currentTarget.childNodes; var childid2 = c[1].id; parent1.replaceChild(c[1], document.getElementById(childid1)); var parent2 = ev.currentTarget; parent2.removeChild(c[1]); parent2.appendChild(document.getElementById(childid1)); } </script> </head> <body> <div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"> <img id="drag1" src="img_logo.gif" draggable="true" ondragstart="drag(event)" width="336" height="69"> </div> <br> <div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)"> <img id="drag2" src="img_logo.gif" draggable="true" ondragstart="drag(event)" width="236" height="49"> </div> </body>
I tried swapping two elements inside div1 and div2 tag but only the first replace function executes properly the other element is not swapped
either one of the div is getting swapped at once but when i try to swap both at same time it doesn't happen please help me out *
<head> <style type="text/css"> #div1 { width:350px; height:70px; padding:10px; border:1px solid #aaaaaa; } #div2 { width:350px; height:70px; padding:10px; border:1px solid #aaaaaa; } </style> <script> function allowDrop(ev) { ev.preventDefault(); } function drag(ev) { var child = ev.target; var parents = child.parentNode; ev.dataTransfer.setData("child1", child.id); ev.dataTransfer.setData("parent1", parents.id); } function drop(ev) { ev.preventDefault(); var childid1 = ev.dataTransfer.getData("child1"); var parentid1 = ev.dataTransfer.getData("parent1"); var parent1 = document.getElementById(parentid1); var c = ev.currentTarget.childNodes; var childid2 = c[1].id; parent1.replaceChild(c[1], document.getElementById(childid1)); var parent2 = ev.currentTarget; parent2.removeChild(c[1]); parent2.appendChild(document.getElementById(childid1)); } </script> </head> <body> <div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"> <img id="drag1" src="img_logo.gif" draggable="true" ondragstart="drag(event)" width="336" height="69"> </div> <br> <div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)"> <img id="drag2" src="img_logo.gif" draggable="true" ondragstart="drag(event)" width="236" height="49"> </div> </body>
1 Answer
Reset to default 9I think your fundamental flaw is that you do not cater for the fact that when you do your first replaceChild
, the image you are inserting is removed from its parent so it is no longer there to replace with the second image.
I found your variable names somewhat confusing, and have rewritten your code in a simpler fashion as follows:
function allowDrop (ev) {
ev.preventDefault ();
}
function drag (ev) {
ev.dataTransfer.setData ("src", ev.target.id);
}
function drop (ev) {
ev.preventDefault ();
var src = document.getElementById (ev.dataTransfer.getData ("src"));
var srcParent = src.parentNode;
var tgt = ev.currentTarget.firstElementChild;
ev.currentTarget.replaceChild (src, tgt);
srcParent.appendChild (tgt);
}
You can test this code at jsFiddle
本文标签: domswapping child elements of div using javascript by drag and dropStack Overflow
版权声明:本文标题:dom - swapping child elements of div using javascript by drag and drop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741236997a2363214.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论