gpt4 book ai didi

css - 如何在拖放每个元素后创建一个类?

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

我正在尝试建立一个这样工作的系统:您可以在列表的 5 个元素之间进行选择,以将其拖放到一个特殊的位置。放下它后,会出现一个弹出窗口,询问您要更改元素的内容。然后修改的元素出现在特殊的地方。

它只工作一次,但如果你想添加另一个元素,这个新元素会删除旧元素以取代它。

我试图在每次删除某些内容时添加一个类,以便不会删除任何内容(因此您将拥有名为 dropped1、dropped2、dropped3 等的类),但不知何故,它似​​乎并没有改变任何东西并且我不明白为什么。

无论如何,这是代码:

$(function() {
$('#allFacets').sortable({
connectWith: 'ul',
delay: 150,
helper: "clone",
placeholder: 'placeholder',
start: function(e, ui) {
ui.item.show(); // force jquery ui to display the original
}
});
$('#userFacets').sortable({
connectWith: 'ul',
delay: 150,
helper: "clone",
placeholder: 'placeholder',
receive: function(e, ui) {
ui.item.clone().appendTo(this); // append a copy
$("#userFacets.userFacets.ui-sortable").each(function(i){
$(this).addClass("received"+ (i+1));
});
var step = prompt("Choose a name");
$(this).html(step);
ui.sender.sortable("cancel"); // return the original
}
}).on("dblclick", "li", function() {
$(this).remove();
});
});
.facet-container {
width: 330px;
}
.right {
float: right;
}
.left {
float: left;
}
p {
clear: both;
padding-top: 1em;
}
.facet-list {
list-style-type: none;
margin: 0;
padding: 0;
margin-right: 10px;
background: #eee;
padding: 5px;
width: 143px;
min-height: 1.5em;
font-size: 0.85em;
}
.facet-list li {
margin: 5px;
padding: 5px;
font-size: 1.2em;
width: 120px;
}
.facet-list li.placeholder {
height: 1.2em
}
.facet {
border: 1px solid #bbb;
background-color: #fafafa;
cursor: move;
}
.facet.ui-sortable-helper {
opacity: 0.5;
}
.placeholder {
border: 1px solid orange;
background-color: #fffffd;
}
<link rel='stylesheet' href='//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/themes/smoothness/jquery-ui.css'>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.1/jquery-ui.min.js"></script>

<div class="facet-container">
<div class="left">
<label>All Facets</label>
<ul id="allFacets" class="facet-list">
<li class="facet">Facet 2</li>
<li class="facet">Facet 3</li>
<li class="facet">Facet 5</li>
<li class="facet">Facet 1</li>
<li class="facet">Facet 4</li>
</ul>
</div>
<div class="right">
<label>User Facets</label>
<ul id="userFacets" class="facet-list">

</ul>
</div>
</div>
<p>Drag & drop to rearrange items within a list or between lists.</br>Double-click to move item from one list to the bottom of the other.</p>

提前致谢!

最佳答案

查看修改后的JS

$(function() {
$('#allFacets').sortable({
connectWith: 'ul',
delay: 150,
helper: "clone",
placeholder: 'placeholder',
start: function(e, ui) {
ui.item.show(); // force jquery ui to display the original
}
});
$('#userFacets').sortable({
connectWith: 'ul',
delay: 150,
helper: "clone",
placeholder: 'placeholder',
receive: function(e, ui) {
var step = prompt("Choose a name");
$(this).append("<li class='facet'>" + step + "</li>")
ui.sender.sortable("cancel"); // return the original
}
}).on("dblclick", "li", function() {
$(this).remove();
});
});
.facet-container {
width: 330px;
}
.right {
float: right;
}
.left {
float: left;
}
p {
clear: both;
padding-top: 1em;
}
.facet-list {
list-style-type: none;
margin: 0;
padding: 0;
margin-right: 10px;
background: #eee;
padding: 5px;
width: 143px;
min-height: 1.5em;
font-size: 0.85em;
}
.facet-list li {
margin: 5px;
padding: 5px;
font-size: 1.2em;
width: 120px;
}
.facet-list li.placeholder {
height: 1.2em
}
.facet {
border: 1px solid #bbb;
background-color: #fafafa;
cursor: move;
}
.facet.ui-sortable-helper {
opacity: 0.5;
}
.placeholder {
border: 1px solid orange;
background-color: #fffffd;
}
<link rel='stylesheet' href='//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/themes/smoothness/jquery-ui.css'>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.1/jquery-ui.min.js"></script>

<div class="facet-container">
<div class="left">
<label>All Facets</label>
<ul id="allFacets" class="facet-list">
<li class="facet">Facet 2</li>
<li class="facet">Facet 3</li>
<li class="facet">Facet 5</li>
<li class="facet">Facet 1</li>
<li class="facet">Facet 4</li>
</ul>
</div>
<div class="right">
<label>User Facets</label>
<ul id="userFacets" class="facet-list">

</ul>
</div>
</div>
<p>Drag & drop to rearrange items within a list or between lists.</br>Double-click to move item from one list to the bottom of the other.</p>

关于css - 如何在拖放每个元素后创建一个类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27402669/

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