简体   繁体   English

在 div 之间来回移动和/或拖动元素并动态捕获它的 div 字符串

[英]Move and/or drag element back and forth from div to div and capture it's div string dynamically

I'm studying this thread : How to move an element into another element?我正在研究这个线程: 如何将一个元素移动到另一个元素中? , and https://www.w3schools.com/html/tryit.asp?filename=tryhtml5_draganddrop , my problem is how do we do this back-and-forth?https://www.w3schools.com/html/tryit.asp?filename=tryhtml5_draganddrop ,我的问题是我们如何来回执行此操作?

This works perfectly as it is.这完美地工作。 I can drag the strings back and forth.我可以来回拖动琴弦。

My goal is for it to look like this.我的目标是让它看起来像这样。

Move all to parent div全部移动到父div

全部移至父级

Move all to child div全部移动到子div

全部转移到孩子身上

But when I try to move it using buttons,但是当我尝试使用按钮移动它时

<button type="button" onclick="Parent()">
    Move From Parent to Child
</button>

<button type="button" onclick="Child()">
    Move From Child to Parent
</button>

This is the current result.这是目前的结果。

当前结果

It just swaps the strings.它只是交换字符串。 It's supposed to merge all together, and not swap.它应该合并在一起,而不是交换。 Is there a way for this to be fixed?有没有办法解决这个问题?

Move from parent to child从父母转移到孩子

从父母转移到孩子

Move from child to parent从孩子转移到父母

从孩子转移到父母

How do we move the strings only from one div to another (vise-versa)?我们如何将字符串仅从一个 div 移动到另一个(反之亦然)?

And lastly, how do we capture span values dynamically?最后,我们如何动态捕获跨度值?

I understand that this works by calling all the values inside <span>我知道这是通过调用<span>所有值来实现的

    $(document).ready(function() {
    var capturevalueschild = $.map($("#child span"), function(elem, index) {
    return $(elem).text();
    }).join("-");
    $("#displayvalueschild").text(capturevalueschild);
    });

    $(document).ready(function() {
    var capturevaluesparent = $.map($("#parent span"), function(elem, index) {
    return $(elem).text();
    }).join("-");
    $("#displayvaluesparent").text(capturevaluesparent );
    });

But my problem is, if I'm going to change the value?但我的问题是,如果我要更改值? It does not capture the latest string being changed.它不会捕获正在更改的最新字符串。

This is what I got so far.这是我到目前为止所得到的。

My Style我的风格

    <style>
        .div-to-drag {
            width: 350px;
            height: 70px;
            padding: 10px;
            border: 1px solid #aaaaaa;
            overflow: scroll;
        }

        #parent {
            height: 100px;
            width: 300px;
            background: green;
            margin: 0 auto;
            overflow: scroll;
        }

        #child {
            height: 100px;
            width: 300px;
            background: blue;
            margin: 0 auto;
            overflow: scroll;
        }
    </style>

My HTML.我的 HTML。

    <div id='parent' class='div-to-drag' ondrop='drop(event)' ondragover='allowDrop(event)'>
        <?php echo "<span id='div1parent' draggable='true' ondragstart='drag(event)'>First Parent<br></span>"; ?>
        <?php echo "<span id='div2parent' draggable='true' ondragstart='drag(event)'>Second Parent<br></span>"; ?>
        <?php echo "<span id='div3parent' draggable='true' ondragstart='drag(event)'>Third Parent<br></span>"; ?>
    </div>
    <br>

    <div id='child' class='div-to-drag' ondrop='drop(event)' ondragover='allowDrop(event)'>
        <?php echo "<span id='div1child' draggable='true' ondragstart='drag(event)'>First Child<br></span>"; ?>
        <?php echo "<span id='div2child' draggable='true' ondragstart='drag(event)'>Second Child<br></span>"; ?>
        <?php echo "<span id='div3child' draggable='true' ondragstart='drag(event)'>Third Child<br></span>"; ?>
    </div>

<div id="result"></div>
<br>
<div id="result"></div>

    <br>

    <button type="button" onclick="Parent()">
        Move From Parent to Child
    </button>

    <button type="button" onclick="Child()">
        Move From Child to Parent
    </button>

My script.我的剧本。

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

        function Parent() {
            $("#parent").insertAfter($("#child"));
        }

        function Child() {
            $("#child").insertAfter($("#parent"));
        }

$(document).ready(function() {
    var capturevalueschild = $.map($("#child span"), function(elem, index) {
    return $(elem).text();
    }).join("-");
    $("#displayvalueschild").text(capturevalueschild);
    });

    $(document).ready(function() {
    var capturevaluesparent = $.map($("#parent span"), function(elem, index) {
    return $(elem).text();
    }).join("-");
    $("#displayvaluesparent").text(capturevaluesparent );
    });
    </script>

To summarize, what I'm trying to achieve here are the following:总而言之,我在这里尝试实现的目标如下:

  1. Enable transferring all the string from 1 div to another, just append it, and vise versa.启用将所有字符串从 1 div 传输到另一个 div,只需附加它,反之亦然。
  2. Dynamically capture data from "parent" div and "child" div, depending on what the data is being stored, transferred there.从“父”div 和“子”div 动态捕获数据,具体取决于数据在那里存储和传输的内容。
  3. Capture the data from "parent" div and convert it into an array so that I can insert it into my database.从“父”div 捕获数据并将其转换为数组,以便我可以将其插入到我的数据库中。

Thank you so much in advance.非常感谢你。

So if i've understood correctly you wish to move all the elements to either child or parent div, not just swapping the elements.因此,如果我理解正确,您希望将所有元素移动到子或父 div,而不仅仅是交换元素。

What you can do is to append the elements.您可以做的是附加元素。 the insertAfter puts the element after the div, just as you did with the drop element. insertAfter 将元素放在 div 之后,就像你对 drop 元素所做的一样。 I've Chosen to loop through the id's on the elements that was to be moved by the click event, and then append these to the parent or child DIV element.我选择循环遍历要由单击事件移动的元素上的 id,然后将它们附加到父或子 DIV 元素。 I have changed the ID's for semantic reason in for coding the loop.出于语义原因,我已更改 ID 以对循环进行编码。 This should solve your problem.这应该可以解决您的问题。

function Parent() {
for (var i = 1; i <= 3; i++) {
    $("#parent").append($("#parentElement" + i));
    $("#parent").append($("#childElement" + i));
    }
}
function Child() {
for (var i = 1; i <= 3; i++) {
    $('#child').append($("#parentElement" + i));
    $("#child").append($("#childElement" + i));   
    }
}

Also, I see no reason using the php tags and echo for the span elements, works fine without it :)另外,我认为没有理由对 span 元素使用 php 标签和 echo,没有它也能正常工作:)

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

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