gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 02:55:32 26 4
gpt4 key购买 nike

在我撕掉最后一根头发之前,我想问问作为最后的手段。目前我正在展示最初的 2 个 div。然后,用户可以根据需要多次将这两个 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>

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

最佳答案

好吧,在你扯掉你的头发之前,看看这个“拖放” fiddle - 它是我之前制作的一个 jsfiddle.net/RachGal/ahnx7kwc它会让您了解整个拖放操作的工作原理 – Rachel Gallen 2 天前

来自这个 fiddle 的 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>

来自这个 fiddle 的 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;}

关于javascript - 使用 Jquery 问题垂直重新排序 Div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37245667/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com