【发布时间】: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