Javascript jQuery UI可排序-替换已删除项的内容
我有三个模块,“可拖动”、“可排序”和“可拖放” 在“draggable”中,我有几个像这样构建的元素:Javascript jQuery UI可排序-替换已删除项的内容,javascript,jquery,jquery-ui,jquery-ui-sortable,Javascript,Jquery,Jquery Ui,Jquery Ui Sortable,我有三个模块,“可拖动”、“可排序”和“可拖放” 在“draggable”中,我有几个像这样构建的元素: <li class="draggable ui-draggable ui-draggable-handle" id="header-1"> <img src="http://placehold.it/150x100"> </li> 这是将“.testing”类添加到当前拖动的项中 我将此添加到“排序表”: receive:函数(事件、用户界面){ $(
<li class="draggable ui-draggable ui-draggable-handle" id="header-1">
<img src="http://placehold.it/150x100">
</li>
这是将“.testing”类添加到当前拖动的项中
我将此添加到“排序表”:
receive:函数(事件、用户界面){
$('.testing')。替换为('');
这里的问题是替换整个标记,包括“li”父元素,我只想替换“img”元素
下面是一个的工作原理。很抱歉误解了这个问题。您描述的是:在不修改原始
可拖动的的情况下修改删除的元素(如果我再次误解,请告诉我)
这通常使用helper
选项完成,通常带有一个函数,但是helper:“clone”
也可以工作。这将导致删除可拖动的的克隆。这意味着您可以将选择器限制在当前容器中,而不需要。测试
class(当然,如果它能让你更容易地看到发生了什么,你也可以保留它):
receive:函数(事件、用户界面){
$('.testing',this).替换为('');
//或者。。。
$(this.find('.testing')。替换为('');
//或者没有多余的课程。。。
$(this.find('li.draggable')。替换为(“”);
//或者按照我原来帖子的建议保留li标签。。。
$('li.draggable>img',this);
}
请记住,jQuery的克隆
仅复制DOM节点,其jQuery属性均不保留。删除的帮助程序将不再是可拖动的小部件(尽管在您的情况下,您使用的是可排序的小部件,它仍然允许您拖动它)在接收事件上,获取上一个删除的元素。如果此元素存在,请附加到发送方
$(".connect").on("sortreceive", function(event, ui) {
var prevChild = $(event.target).children().not(ui.item);
if(prevChild.length > 0){
$(ui.sender).append(prevChild);
}
});
我已经试过了,但它仍然会修改“可拖动”块中的图像项,我不想这样做,它应该只修改拖放到“可排序”块中的图像。这似乎可行,但当我将其拖动到“#侧边栏包装器”时,我需要能够删除“可排序”块中拖放的元素这是负责它的代码,您也可以在jsbin中检查它:$(“#侧栏包装”).dropable({accept:'li',drop:function(event,ui){ui.helper.remove();})
这可能吗?我今天又试了好几个小时,好像没法做到这一点?你有什么想法吗?为此,你只需要.remove()
删除ui.draggable
;或者保留
,或者接受:“img”
receive: function (event, ui) {
$('.testing').replaceWith('<img src="http://placehold.it/400x300">');
receive: function (event, ui) {
$('.testing', this).replaceWith('<img src="http://placehold.it/400x300">');
// or...
$(this).find('.testing').replaceWith('<img src="http://placehold.it/400x300">');
// or without the superfluous class...
$(this).find('li.draggable').replaceWith('<img src="http://placehold.it/400x300">');
// or to keep the li tags as per my original post's suggestion...
$('li.draggable > img', this).replaceWith('<img src="http://placehold.it/400x300">');
}
$(".connect").on("sortreceive", function(event, ui) {
var prevChild = $(event.target).children().not(ui.item);
if(prevChild.length > 0){
$(ui.sender).append(prevChild);
}
});