[英]How do you target a cloned draggable element during the 'stop' event?
我有一个可拖动的列表元素,该元素在拖动时克隆并填充另一个列表。 当我删除该项目时,我想更改新删除的li元素的内容。 我的问题是jQuery的这仍然是原始拖动项,而不是克隆项。
如何专门针对新克隆的项目,以便可以更改其内容而不影响列表中的其他项目?
代码如下。 感谢您的智慧!
CSS:
<style>
#intro-clone, #assessment-clone {
list-style-type: none;
margin: 0;
padding: 0;
width: 180px;
display: inline-block;
}
#intro-statements, #assessment-statements {
list-style-type: none;
margin: 0;
padding: 0;
width: 365px;
}
#intro-clone li, #assessment-clone li, #intro-statements li, #assessment-statements li {
margin: 0 5px 5px 5px;
padding: 5px;
font-size: 1.2em;
min-height: 25px;
}
#intro-clone li, #assessment-clone li, #intro-statements li, #assessment-statements li {
min-height: 25px;
line-height: 1.2em;
}
.ui-state-highlight {
min-height: 25px;
line-height: 1.2em;
}
ul {
padding: 6px 1px 1px 0 !important;
min-height: 42px;
background: silver;
border-radius: 3px;
}
</style>
JS:
<script>
$(function() {
$("#intro-statements, #assessment-statements").sortable({
placeholder: "ui-state-highlight",
revert: true
});
$( "#intro-clone li" ).draggable({
drag: function() { $("#intro-clone li").css("width","158px"); },
stop: function() {
$("#intro-statements li").css({"width":"343px","height":"25px"}); // this is applied to ALL the li elements, including the cloned one
console.log(this); // reference's the original drag element, not the clone
window.setTimeout(function() { $(this).html("new content") }, 1000); // doesn't work!
},
connectToSortable: "#intro-statements",
helper: "clone",
revert: "invalid"
});
$( "#assessment-clone li" ).draggable({
drag: function() { $("#assessment-clone li").css("width","158px"); },
stop: function() {
$("#assessment-statements li").css({"width":"343px","height":"50px"}).html("new content<br>new line"); // this is applied to ALL the li elements, including the cloned one
},
connectToSortable: "#assessment-statements",
helper: "clone",
revert: "invalid",
});
$("#intro-statements, #assessment-statements").disableSelection();
$("#show").click(function() {
var intros = {};
$('li', 'ul.sort').each(function(i) {
var $li = $(this);
var $text = $li.text();
var name = $li[0].tagName.toLowerCase();
//intros[name + '-' + i] = $text;
intros[i] = $text;
});
console.log(intros);
$("#show").html(JSON.stringify(intros));
});
});
</script>
HTML:
<ul id="intro-clone">
<li class="ui-state-default">Intro item</li>
</ul>
<ul id="assessment-clone">
<li class="ui-state-default">Assessment item</li>
</ul>
<br>
<br>
<ul id="intro-statements" class="sort">
</ul>
<br>
<ul id="assessment-statements" class="sort">
</ul>
<div id="show">SHOW</div>
疯狂地花了很多时间试图找到解决方案,然后当您寻求帮助时,它就来了!
因此,这为我带来了窍门:我将一个类附加到drag事件上的元素上。 删除新元素后,我从源元素中删除该类,并将该类定位到新克隆的元素上,然后对其进行更改。 最后,我然后从克隆的元素中删除该类,以使其保持唯一。
这是更新的js:
$( "#intro-clone li" ).draggable({
drag: function() {
$("#intro-clone li").css("width","158px").addClass("new"); // add the new class
},
stop: function() {
$("#intro-clone li").removeClass("new"); // remove it again
$("#intro-statements li.new").css({"width":"343px","height":"25px"});
window.setTimeout(function() { $("#intro-statements li.new").html("new content") }, 1000); // make my changes to the cloned element
window.setTimeout(function() { $("#intro-statements li").removeClass("new") }, 1500); // remove class so it is unique for the next clone
},
connectToSortable: "#intro-statements",
helper: "clone",
revert: "invalid"
});
这可能是好的代码,可能不是,但是它解决了我的问题,所以我很高兴:)
您需要将stop
添加到以下sortable
方法中,因为sortable
使得draggable
和droppable
draggable
在一起。 我认为这对您来说更好。
$("#intro-statements, #assessment-statements").sortable({
placeholder: "ui-state-highlight",
revert: true,
stop: function(event, ui) {
console.log(event.target);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.