使用 Jquery 问题垂直重新排序 Div

Reorder Divs Vertically using Jquery Issue

在我拔掉最后一根头发之前,我是最后一招。目前我正在展示最初的 2 divs。然后,用户可以根据需要在原始文件下方添加这两个 div 任意多次。如有必要,他们还可以删除 2 div。我正在尝试允许使用拖放对 divs 进行重新排序。我尝试了很多方法,但就是无法正常工作。作为额外说明,divs 需要在用户拖放后重新索引自己。这是我的代码。请注意,在确定这个基本实现之前,我已经添加和删除了很多代码尝试。提前致谢。

加载JQuery

首先向最终用户显示 div

            <!--Div contains each answer step-->
            <div id = "answer_step" class = "answer_step">
                <!--This placeholder text is styled from CSS and will empty the div once the user starts typing-->
                <div id="answer_step_equation0" class="answer_step_equation" contenteditable="true" placeholder="Enter The Next Solution Step This Question"></div>
                <div id="answer_step_description0" class = "answer_step_description" contenteditable="true" placeholder="Enter A Description as to how this step was reached or how to solve the next step"></div>
            </div>

        </div>
<!-- Buttons to dynamically add and remove answer divs. The remove functionality is added in JQuery for the add button-->
        <button id="add_answer_step_button" class="add_answer_step_button">+ Add next Step</button>

此代码附加了新的 div。我必须像上面那样将三个 div 放在 div 中,但我无法让它工作。

<!--Script to append/remove div when user clicks on add_answer_step_button-->
<script type = "text/javascript" language = "javascript">

    $(document).ready(function() {
        <!--This variable gives each new set of answer divs a unique id by appending a number to the end of th id-->
        <!--The first set of answer divs will have an id of zero-->
        var answer_identifier_number = 1;
        $("button.add_answer_step_button").click(function () {
            $("div.answer_steps").append('<div id="answer_step_equation' + answer_identifier_number + '" class="answer_step_equation" contenteditable="true" placeholder="Enter The Next Solution Step This Question"></div>');
            $("div.answer_steps").append('<div id="answer_step_description' + answer_identifier_number + '" class = "answer_step_description" contenteditable="true" placeholder="Enter A Description as to how this step was reached or how to solve the next step"></div>');
            $("div.answer_steps").append('<button id="remove_answer_step_button' + answer_identifier_number + '" class = "remove_answer_step_button">- Remove This Step</button>');

            answer_identifier_number++;
        });
    });

</script>

允许 div 可拖动

<script type = "text/javascript" language = "javascript">
$(function() {  
    $( "#answer_steps" ).sortable();
    $( "#answer_steps" ).disableSelection();
    cursor: move;
});
</script>

我还没有想出如何重新索引名称,但我有这方面的经验,所以我应该没问题。感谢您的帮助。

好吧,在你撕毁你的头发之前,看看这个 'drag and drop' fiddle - 这是我早先做的一个 jsfiddle.net/RachGal/ahnx7kwc 它会让您了解整个拖放操作的工作原理 – Rachel Gallen 2 天前

HTML从这个fiddle

<div class="common">
<div class="container-fluid">
    <div class="row">
            <ul class="col-sm-3 col-md-2 sidebar nav nav-sidebar" >
            <li class="group" class="draggable" ><a href="#" class="list-group-item">
            <h4>
             dsad
             <br /><small>dsadsa</small>
             <br /><small>dsadsa</small>
            </h4>
          </a>
                </li>
                <li class="group" class="draggable"><h2>
                BAH
                </h2><br><small>hello</small>
            </ul>
         <div id="drophere" class="col-sm-9 col-md-9">MAIN PANEL</div>
    </div>
</div>

Javascript从这个fiddle

$(".draggable").draggable();
var draggableArguments={
 revert: 'invalid',
 helper:'clone',
 appendTo: '#drophere',
 refreshPositions: true,
 containment: 'DOM',
 zIndex: 1500,
 addClasses: false
};

$('.group').draggable(draggableArguments);
$('.group').droppable();

$(".nav-sidebar").droppable({
 tolerance: "intersect",
 accept: ".group",
 activeClass: "ui-state-default",
 hoverClass: "ui-state-hover",
 drop: function(event, ui) {
    $(".nav-sidebar").append($(ui.draggable));
  }
});
$('#drophere').droppable({
 tolerance: "intersect",
 accept: ".group",
 activeClass: "ui-state-default",
 hoverClass: "ui-state-hover",
 drop: function(event, ui) {        
    $('#drophere').append($(ui.draggable));
  }
});
$('#drophere').sortable();

CSS从这个Fiddle

.draggable {
  border: solid 2px gray;
}
.sidebar {
  position:fixed;
  width:200px;
  top: 51px;
  bottom: 0;
  left: 0;
  z-index: 1000;
  display: block;
  padding: 20px;
  overflow-y: auto;
  /* Scrollable contents if viewport is shorter than content. */
  overflow-x: visible;
  background-color: #f5f5f5;
  white-space: nowrap;
  border-right: 1px solid #eee;
  padding-left: 30px;
  padding-right: 30px;
}
/* Sidebar navigation */
.nav-sidebar {
  width:200px;
  height:100vh;
  margin-right: -21px;
  /* 20px padding + 1px border */
  margin-bottom: 20px;
  margin-left: -20px;
}
.group{width:150px;
  height:auto;
}

#drophere{width:700px;left:200px; height:100vh;}