简体   繁体   English

HTML5 + Javascript拖放

[英]HTML5 + Javascript drag n drop

my question is based kind of on this example but there are some differences.. http://jsfiddle.net/raad/SSxdB/15/ 我的问题基于该示例,但有一些区别。.http://jsfiddle.net/raad/SSxdB/15/

i use a menu, which has 3-4 categories, and each category, has 4 items.. so i want to drag and drop an element to a specified div..and the specified element, to be hidden from the menu list (i ve done this) BUT be also able to undo it (have figured it out as well) and also, if i drag n drop another element on a specified div, that already another element has been dropped, the first one to be restored, and the new one takes up its place.. and here is were i have the problem.. this is the code.. 我使用的菜单有3-4个类别,每个类别有4个项目。.所以我想将一个元素拖放到指定的div ..和指定的元素,以从菜单列表中隐藏(i已经做到了)但是还可以撤消它(也已经解决了),而且,如果我将n另一个元素拖放到指定的div上,则已经删除了另一个元素,要还原的第一个元素,以及新的取代它的位置..这是我有问题..这是代码..

    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");
        var draggedOrNewNodeId = "dragged-"+data;
        var theNode = document.getElementById(data);
        var draggedNode = document.getElementById(draggedOrNewNodeId);
        if (draggedNode != null) { ev.target.removeChild(draggedNode); }
        var nodeCopy = theNode.cloneNode(true);
        nodeCopy.id = draggedOrNewNodeId;
        nodeCopy.setAttribute("class", "dragged");
        nodeCopy.setAttribute("draggable", "false");
        document.getElementById(data).style.display = "none";
        remove_id = draggedOrNewNodeId +  "_remove";
        nodeCopy.innerHTML="<span class='undo' id='" + remove_id + "'>x</span>";
        ev.target.appendChild(nodeCopy);

        var remove = document.getElementsByClassName("undo").length;
            for (i=0; i<remove; i++) {
            var nodeToBeRemoved = /_remove$/;
            var nodeToBeReplaced = /dragged-/;
            deletedNode = remove_id.replace(nodeToBeRemoved, "");
            restoredNode = deletedNode.replace(nodeToBeReplaced, "");
                document.getElementsByClassName("undo")[i].onclick = function() {
                    elem = document.getElementById(deletedNode);deleted
                    elem.parentNode.removeChild(elem);
                    document.getElementById(restoredNode).style.display = "block";
                }
            }
    }

and this is the part that "causes" me the problem apart, from the fact that if there are 2 or mover divs already placed, they disappear all..the other problem is, that if i insert it in the allowDrop(ev) function, it does actually remove the previous elements, and on drop places the new one..but it removes them when the element is over the div..however i want this to be done when dropped..so when i move this piece of code to drop function, it does, remove the previously entered element, but it "vanishes" the new one.. as well...can't tell why this is happening.. also i would like a suggestion replacing the temp_node = document.getElementsByClassName("dragged")[0]; 这是导致“我”问题分开的部分,事实上,如果已经放置了2个或移动div,它们就会全部消失..另一个问题是,如果我将其插入allowDrop(ev)函数中,它实际上确实删除了先前的元素,并在放置时放置了新元素。.但是当元素在div上时,它将删除它们。但是我希望在放置时完成此操作。所以当我移动这段代码时删除函数,它确实删除了先前输入的元素,但是它“消失了”新元素..以及...不知道为什么会这样..我也想建议替换temp_node = document。 getElementsByClassName(“ draged”)[0]; by sth more specific (perhaps child node from the specified ev.target? 更具体地(也许是来自指定ev.target的子节点?

        tempNodeCheck = hasClass(ev.target, "dragged"); 
        if (tempNodeCheck) {
                var nodeToBeReplacedcopy = /dragged-/;
                tempParentNode = ev.target.parentNode;
                temp_node = document.getElementsByClassName("dragged")[0];
                temp_node1 = document.getElementsByClassName("dragged")[0].id;
                restoredNode = temp_node1.replace(nodeToBeReplacedcopy, "");
                temp_node1.parentNode.removeChild(temp_node1);
                document.getElementById(restoredNode).style.display = "block";

the html looks sth like this HTML看起来像这样

<div id="drag1" draggable="true" ondragstart="drag(event)"></div>
<div id="drag2" draggable="true" ondragstart="drag(event)"></div>
<div id="drag3" draggable="true" ondragstart="drag(event)"></div>
<div id="drag4" draggable="true" ondragstart="drag(event)"></div>

<div class="droparea" id="drop1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div class="droparea" id="drop2" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div class="droparea" id="drop3" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div class="droparea" id="drop4" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div class="droparea" id="drop5" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

here is in jsfiddle sort of http://jsfiddle.net/q5FAq/3/ but also i noticed, that if the removed div contains sth inside, it doesn't get "moved" along with the div.. (currently on what i had tried i had used background images on each div so it wasn't noticeable.. 这是jsfiddle中的一种http://jsfiddle.net/q5FAq/3/,但是我也注意到,如果删除的div包含sth,它不会随div一起“移动”。我已经尝试过在每个div上使用背景图片,所以它并不引人注意。

and here comes the solution.. why the div was disapperaing, when the action was to replace an existing div on the droparea? 解决方案就来了..为什么要替换divarea上的现有div时,为什么div消失了?

because the ev.target, would "see" as target, the existing div inside the droparea.. for example.. 因为ev.target会将“(droparea)内部的现有div作为目标”“例如”。

when an element was dropped on the first place in the drop area, the ev.target was for instance div with id=drop1 (if selected to drop it there) 当元素被放置在放置区域的第一位时,ev.target例如是div,其id=drop1 (如果选择将其放置在其中)

BUT when another element was dropped on top of an element that had previously been dropped in the droparea, then the ev.target would be dragged-drag1 but since the code, was supposed to remove that div, for that reason the recent dropped div got disappeared as well... 但是,当另一个元素放置在先前已放置在droparea中的元素之上时,则将ev.target dragged-drag1但是由于代码应该删除该div,因此最近删除的div得到了也消失了...

so under the line 所以在线下

 ev.target.appendChild(nodeCopy);

gets replaced with this block 被这个方块取代

        tempNodeCheck = hasClass(ev.target, "dragged");
        if (tempNodeCheck) {
                var nodeToBeReplacedcopy = /dragged-/;
                tempParentNode = ev.target.parentNode;
                temp_node_class = tempParentNode.getElementsByClassName("dragged")[0];
                temp_node_id = temp_node_class.id;
                remove_node = document.getElementById(temp_node_id);
                restoredNode = temp_node_id.replace(nodeToBeReplacedcopy, "");  
                remove_node.parentNode.removeChild(remove_node);
                document.getElementById(restoredNode).style.display = "block";
                tempParentNode.appendChild(nodeCopy);
        }
        else {
        ev.target.appendChild(nodeCopy);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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