【问题标题】:Reorder Divs Vertically using Jquery Issue使用 Jquery 问题垂直重新排序 div
【发布时间】:2016-05-16 01:58:56
【问题描述】:

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

加载 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>

我还没有弄清楚如何重新索引名称,但我有这部分的经验,所以我应该没问题。感谢您的帮助。

【问题讨论】:

  • 你能设置一个小提琴来证明它不起作用。您提供的代码似乎缺少一些部分。
  • 我在这里添加了一个 jsfiddle jsfiddle.net/max_m/wuqvngor 谢谢
  • 感谢 Rachel,我现在已经完成了一半工作(一切都是可拖放的 grrrr。拖放的编码方法仍然完全让我无法理解,这通常不会发生。我会尝试明天晚上我有时间睡觉时再来一次。再次感谢您的快速回复。
  • 搞定了@RachelGallen 谢谢

标签: javascript jquery html css draggable


【解决方案1】:

好的,在你把头发扯掉之前,看看这个“拖放”小提琴 - 它是我之前制作的一个继承人 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();

这个小提琴的 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;}

【讨论】:

  • @rachel-gallan 大家好,我一直在尝试使用 Rachel 的代码并将其合并到我的代码中,但我无法让它工作。这是用于添加和删除 div 的小提琴 jsfiddle.net/5r07utj1/10。我已经尝试了许多基于 Rachel 代码的方法,但我觉得我已经没有选择余地了,但基于此评论中的小提琴再次请求您的帮助。再次感谢任何帮助。附言在这个阶段我几乎会秃顶/
  • 道歉瑞秋。仍在学习绳索。我没有收到任何电子邮件。我以后会更加小心,并在船上听取您的建议。感谢上述解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-01
  • 2013-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-21
  • 1970-01-01
相关资源
最近更新 更多