拖放不会将样式附加到div

时间:2018-04-27 18:44:22

标签: javascript html5 jquery-ui-draggable

我试图拖放一个div,但造型并没有随之而来。我做错了什么?

<script>
    function _(id) {
        return document.getElementById(id);
    }

    function allowDrop(ev) {
        ev.preventDefault();
    }
    function drag_start(ev) {
        ev.dataTransfer.setData("elem_id", ev.target.id);
    }

    function drop(ev) {
        ev.preventDefault();
        var data = ev.dataTransfer.getData("elem_id");//returns external card id
        //var which_div = ev.target.id;//ev.target.id; //eg div2

        console.log("div: " + ev.target.id + " object dropping: " + ev.dataTransfer.getData("elem_id"))

        for (var i = 0; i < ev.target.children.length; i++) {
            console.log(ev.target.children[i].id + " is in the drop zone");
        }
        //ev.target.appendChild(_(ev.dataTransfer.getData("elem_id"))); //Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.
        ev.target.append(_(ev.dataTransfer.getData("elem_id"))); //appends [Object] not the styling

    }
</script>

请注意[对象]不是正确的卡片样式(在右侧) enter image description here

1 个答案:

答案 0 :(得分:0)

您的代码没有明显的错误。下面的脚本适用于Firefox和Chrome以及代码段运行程序。这是你的代码在中间。

&#13;
&#13;
<!DOCTYPE HTML>
<html>
<head>
    <style>
        .draggable {
            width: 140px;
            height: 140px;
            background: palevioletred;
            border-radius: 10px;
        }
        .draggable.is-dragging {
            opacity: 0.7;
        }
        #div1 {
            width: 200px;
            height: 200px;
            padding: 10px;
            border: 1px solid #aaaaaa;
        }
    </style>
    <script>
        function _(id) {
            return document.getElementById(id);
        }

        function allowDrop(ev) {
            ev.preventDefault();
        }
        function drag_start(ev) {
            ev.dataTransfer.setData("elem_id", ev.target.id);
        }

        function drop(ev) {
            ev.preventDefault();
            var data = ev.dataTransfer.getData("elem_id");//returns external card id
            //var which_div = ev.target.id;//ev.target.id; //eg div2

            console.log("div: " + ev.target.id + " object dropping: " + ev.dataTransfer.getData("elem_id"))

            for (var i = 0; i < ev.target.children.length; i++) {
                console.log(ev.target.children[i].id + " is in the drop zone");
            }
            //ev.target.appendChild(_(ev.dataTransfer.getData("elem_id"))); //Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.
            ev.target.append(_(ev.dataTransfer.getData("elem_id"))); //appends [Object] not the styling

        }
    </script>
</head>
<body>
    <p>Drag the image into the rectangle:</p>
    <div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    <div id="mypurplerect" class="draggable" draggable="true" ondragstart="drag_start(event)"></div>
</body>
</html>
&#13;
&#13;
&#13;

还有其他事情发生吗?