需要帮助来重新启用可拖动对象的拖动

发布于 2024-09-26 03:34:13 字数 519 浏览 4 评论 0原文

我有一个可拖动元素

$(".element").draggable({
helper: "clone",
revert: 'invalid',
containment: '#parent',
appendTo: '#parent'
});

我有两个问题:

  1. 删除此元素后,原始元素会自动禁用。

  2. 关闭锚标记将附加到放置的元素上。单击此“关闭”后,原始元素应再次变为可拖动,并应从可放置的“div”中删除。

我已经为关闭锚点编写了一个处理程序,如下所示,它从可放置元素中删除元素,但不会使其再次可拖动。

$('.cancel a',ui.helper).click(function()
{
   $(ui.helper).remove();
   $(ui.draggable).draggable('enable');
});

请帮忙。 提前致谢。

I have a draggable element

$(".element").draggable({
helper: "clone",
revert: 'invalid',
containment: '#parent',
appendTo: '#parent'
});

I have two issues:

  1. After this element is dropped, the original gets disabled automatically.

  2. A close anchor tag is appended to the dropped element. On click of this 'close' the original element should again become draggable and should be removed from the droppable 'div'.

I have written a handler for the close anchor as follows, it removes the element from the droppable but it doesn't make it draggable aggain.

$('.cancel a',ui.helper).click(function()
{
   $(ui.helper).remove();
   $(ui.draggable).draggable('enable');
});

Please help.
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

铁憨憨 2024-10-03 03:34:13

第一个问题的答案是将可拖动元素的禁用选项设置为 false。

    $(".element").draggable({
      helper: "clone",
      revert: 'invalid',
      disabled: false,
      containment: '#parent',
      appendTo: '#parent'
    });

在关闭锚点的处理程序上,启用要拖动的可拖动对象。例如,

    $('.cancel a',ui.helper).click(function()
    {
       $(ui.helper).remove();
       $('#element').draggable('enable');
    });

The answer for the first issue is set the disabled option of the draggable element as false.

    $(".element").draggable({
      helper: "clone",
      revert: 'invalid',
      disabled: false,
      containment: '#parent',
      appendTo: '#parent'
    });

On handler for close anchor, enable the draggable object which you want to drag. For example,

    $('.cancel a',ui.helper).click(function()
    {
       $(ui.helper).remove();
       $('#element').draggable('enable');
    });
一身仙ぐ女味 2024-10-03 03:34:13

我假设您在 Drop 事件中禁用了可拖动功能(?)
$(ui.draggable) 仍然在上下文中。
但是,在单击事件中,您无法再访问可拖动对象,而应该直接选择要拖动的元素。例如: $("#element").draggable("enable")

I assume you disabled the draggable in the the Drop event (?)
$(ui.draggable) was still in context there.
however in the click event you cannot access the draggable object anymore and instead you should select the element you want to drag directley. e.g : $("#element").draggable("enable")

半边脸i 2024-10-03 03:34:13

我做了类似的事情,我创建一个数组来保存删除的可拖动对象的索引,如下所示:

var $drop = $(selector for my droppable),
    $drag = $(selector for my draggable),
    dragged = [];
$drop.droppable({
    drop: function (e, ui) {
        var dragIndex = $drag.index(ui.draggable);
        dragged[$drop.index($(this))] = dragIndex;
    }
});

然后要在取消函数中重新启用可拖动对象,您将执行以下操作:

$('.cancel a').click(function() {
    var clickIndex = $(selector for droppable).index($(this).parent()),
        whichDrag = dragged[clickIndex];
    $drag.eq(whichDrag).draggable("enable");
});

我猜该元素$(this).parent() 返回的内容与选择器中可放置元素的元素相匹配,如果不是,则必须进行不同的遍历。

基本思想是找到被拖动项目的索引,将其存储在dragged数组中,其位置等于其被拖放的项目的索引。然后,在取消时,您可以使用可拖动项所在的索引来从与原始可拖动位置匹配的 draged 数组中获取值,然后启用该值。

I've done a similar thing where I create an array to hold the index of the dropped draggable, like this:

var $drop = $(selector for my droppable),
    $drag = $(selector for my draggable),
    dragged = [];
$drop.droppable({
    drop: function (e, ui) {
        var dragIndex = $drag.index(ui.draggable);
        dragged[$drop.index($(this))] = dragIndex;
    }
});

Then to re-enable the draggable in your cancel function, you would do something like this:

$('.cancel a').click(function() {
    var clickIndex = $(selector for droppable).index($(this).parent()),
        whichDrag = dragged[clickIndex];
    $drag.eq(whichDrag).draggable("enable");
});

I'm guessing that the element that is returned by $(this).parent() matches the elements in your selector for the droppable, if not, you'll have to do a different traversal.

The basic idea is to find the index of the dragged item, store that in the dragged array at the position equal to the index of the item on which it is dropped. Then on cancel, you use the index of the item where the draggable was dropped to get the value from the dragged array that matches the original draggable position and then enable that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文