问题描述
我正在使用一个可视化编辑器,该编辑器要求用户可以根据需要添加,删除和拖动元素.每个元素都是一个div
,可通过jQueryUI拖动.新元素将附加到表示工作空间的父div
上.每个元素内部都有一个按钮可将其删除.这一切都很好.
I'm working on a visual editor that requires elements the user can add, remove, and drag around as they like. Each element is a div
made draggable with jQueryUI. New elements are appended to a parent div
that represents the workspace. Each element has a button inside itself to remove it. This all works great.
我遇到的问题是,当我删除不是最近创建的元素时,所有其他可拖动元素都会更改位置.这似乎是由于使用relative
定位的可拖动元素引起的.
The problem I'm having is that when I remove an element that was not the most recently created, all the other draggable elements change position. This seems to be caused by the draggable elements using relative
positioning.
此刻,我的删除功能只是调用$('#item-x').remove()
.我还有另一种方法可以删除可拖动元素吗?
At the moment, my removal function simply calls $('#item-x').remove()
. Is there another way I should be removing draggable elements?
推荐答案
结果是此问题与,但是我在找错东西.
Turns out this question is a duplicate of this one, but I was searching for the wrong thing.
解决方案是根据其他问题,简单地将元素更改为具有绝对位置.但是,我发现,即使我在CSS中将元素指定为绝对位置,我的元素也将始终恢复为相对位置,所以现在我这样做了:
The solution is to simply change the elements to have absolute positioning, as per the other question. I found, however, that my elements would always revert to relative positioning even when I specified in their CSS that they should be absolute, so now I do:
$('#item-x').draggable().css("position", "absolute");
这篇关于jQueryUI:正确删除可拖动元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!