使用Jquery Issue垂直重新排序Divs

Reorder Divs Vertically using Jquery Issue

本文关键字:排序 Divs 新排序 Jquery Issue 垂直 使用      更新时间:2023-09-26

在我把最后一根头发扯掉之前,我是在万不得已的情况下问的。目前我正在展示一个初始的2个div。然后,用户可以根据需要多次在原始div下面添加这两个div。如果需要,他们也可以移除2个div。我正试图允许使用拖放重新调用div。我试过很多方法,但就是不能奏效。另外需要注意的是,一旦用户拖放,div就需要重新索引自己。这是我的密码。在确定这个基本实现之前,请考虑到我已经添加和删除了很多代码尝试。提前谢谢。

加载JQuery

向最终用户显示第一个div

            <!--Div contains each answer step-->
            <div id = "answerswer_step" class = "answerswer_step">
                <!--This placeholder text is styled from CSS and will empty the div once the user starts typing-->
                <div id="answerswer_step_equation0" class="answerswer_step_equation" contenteditable="true" placeholder="Enter The Next Solution Step This Question"></div>
                <div id="answerswer_step_description0" class = "answerswer_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="answerswer_step_equation' + answer_identifier_number + '" class="answerswer_step_equation" contenteditable="true" placeholder="Enter The Next Solution Step This Question"></div>');
            $("div.answer_steps").append('<div id="answerswer_step_description' + answer_identifier_number + '" class = "answerswer_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>

我还没想好如何重新命名,但我有这方面的经验,所以我应该没事。谢谢你的帮助。

好吧,在你把头发扯掉之前,先看看这个"拖放"小提琴——这是我以前做的一个jsfiddle.net/RachGal/ahnx7kwc它会让你了解整个拖放的工作原理——Rachel Gallen 2天前

这个小提琴的HTML

<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

$(".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();

这个Fiddle 的CSS

.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;}