javascript - 使用 JQuery UI 拖放以填充空白

标签 javascript jquery html css jquery-ui

下面是我的场景解释截图,

enter image description here

从上面提到的图片。棕色框位置被定义为可放置,下面的选择是可拖动的。如果我拖放它工作正常。

enter image description here

例如,如果我将另一个选项拖动到已定义的位置,则其重叠

enter image description here

我想做的是它将文本替换为“受人尊敬”,并且已经删除的文本“supercious”将返回到下面框中的选择文本。我该怎么做。请任何人帮助我摆脱这个问题。 下面是我的代码

  <!doctype html> 
<html lang="en"> 
<head> 
<meta charset="utf-8"> 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
<title>jQuery UI Droppable - Default functionality</title> 
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
<style> 
.box1
{
background: #E7AC9F;
height: 20px;
width: 100px;
display: inline-block;
font-size: 16px;
margin-left: 10px;
margin-right: 10px;
}
.di1{
    border: 1px dotted #22BAA0;
    margin: 10px;
    padding: 10px;
    width: 100px;
    border-radius: 4px;
    font-size: 14px;
    cursor: move;
    list-style: none;
    display: inline-block;
}
</style> 
 <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script> 
$( function() {
$(".box1").sortable();
    $(".box1").disableSelection();

    $(".qitem2").draggable({
        containment : "#container",
        helper : 'clone',
        revert : 'invalid'
    });

    $(".box1, #qlist2").droppable({
        hoverClass : 'ui-state-highlight',
        accept: ":not(.ui-sortable-helper)",
        drop : function(ev, ui) {
            $(ui.draggable).clone().appendTo(this);
            $(ui.draggable).remove();
            $(ui.draggable).removeAttr("style");

        }
    });
    });
</script> 
</head> 
<body> 
<p>The multi-coloured eyes,<div class="box1"></div>and striking facial cuts and curves<div class="box1"></div>  the Siberian Husky as one of the most desirable and breeds <div class="box1"></div>of dogs. Originally from Siberia, they look like replicas of wolves; also, they are challenging to new owners due to their agility and<div class="box1"></div></p>

<div id="qlist2">
    <div class="qitem2 di1">
        Option 1
    </div>
    <div class="qitem2 di1">
       Option 2
    </div>
    <div class="qitem2 di1">
        Option 3
    </div>
    <div class="qitem2 di1">
        Option 4
    </div>
     <div class="qitem2 di1">
        Option 5
    </div>
     <div class="qitem2 di1">
        Option 6
    </div>
     <div class="qitem2 di1">
        Option 6
    </div>
</div>
</body> 
</html>

最佳答案

只需为 draggables 元素创建一个容器,这样每当第二个元素被放入 droppable 中时,我们就可以将第一个元素移动到它的容器中

HTML:

 <div id="qlist2">
  <div id = "dragitem1-container" class="drag-container">
  <div id="dragitem1" class="qitem2 di1">
    Option 1
  </div>
</div>

<div id = "dragitem2-container" class="drag-container">
  <div id="dragitem2" class="qitem2 di1">
    Option 2
  </div>
</div>

<div id = "dragitem3-container" class="drag-container">
   <div id="dragitem3" class="qitem2 di1">
       Option 3
   </div>
</div>
</div>

<div class="droppable-element">
</div>

JS:

$(document).ready(function() {

function makedraggable() {
 $(".qitem2").draggable({
        "revert": "invalid"
 });

makedraggable();

 $(".droppable-element").droppable({
    "accept": ".qitem2",
    "drop": function(event, ui) {
        if ($(this).find(".qitem2").length) {
            var $presentChild = $(this).find(".qitem2"),
                currChildId = $presentChild.attr("id"),
                $currChildContainer = $("#" + currChildId + "-container");                  
           $currChildContainer.append($presentChild);
           $presentChild.removeAttr("style");
           makedraggable();
        }

        $(ui.draggable).clone().appendTo(this).removeAttr("style");
        $(ui.draggable).remove();

    }
 })
})

我想这就是你想要的

https://jsfiddle.net/4bL4tfrt/

关于javascript - 使用 JQuery UI 拖放以填充空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41425515/

相关文章:

javascript - Backbone : Feed JSON in a variable instead of fetching through URL

javascript - 如何使用jquery检查输入是否检查

jquery - 一个 Jquery 对话框可以打开另一个 Jquery 对话框吗?

html - Bootstrap Navbar 与 Google 重叠放置自动完成下拉菜单

javascript - 如果元素具有名称已测试的类,则显示该元素

javascript - jQuery - 如何选择变量的后缀并在数组中找到它的匹配项

javascript - bootstrap-table 中的 table-multiple-sort 不适用于一页中的多个表

jquery - Backbone.js View 中更灵活的 tagName

javascript - 如何在winjs上制作一个splitview菜单

javascript - 使用javascript在html上显示和隐藏文本