Jquery 拖放和克隆

标签 jquery drag-and-drop

嗨,我需要实现这个目标..

我有一组可转换的元素(基本上我是在服装上转换设计)并且我正在转换一个克隆..

如果我不喜欢放置的对象(设计) - 我想通过执行隐藏之类的操作来删除它。

但我无法做到这一点。

请帮助我..

<小时/>

这是代码

    var clone;
    $(document).ready(function(){
        $(".items").draggable({helper: 'clone',cursor: 'hand'});


     $(".droparea").droppable({
                    accept: ".items",
                    hoverClass: 'dropareahover',
                    tolerance: 'pointer',
                    drop: function(ev, ui)
                    {

              var dropElemId = ui.draggable.attr("id");

              var dropElem = ui.draggable.html();

                      clone = $(dropElem).clone(); // clone it and hold onto the jquery object
                      clone.id="newId";
                      clone.css("position", "absolute");
          clone.css("top", ui.absolutePosition.top);
                      clone.css("left", ui.absolutePosition.left);
              clone.draggable({ containment: 'parent' ,cursor: 'crosshair'});

                      $(this).append(clone);
                      alert("done dragging ");

                      /lets assume I have a delete button when I click that clone should dissapear so that I can drop another design - but the following code has no effect 
                      //and the item is still visible , how to make it dissapear ?
                      $('#newId').css("visibility","hidden");



               }
        });



    });

最佳答案

因为 .clone() 返回一个 jQuery 对象。 clone.id="newId"在 jQuery 对象而不是 DOM 元素上设置属性。由于 DOM 元素没有 id 属性。 $('#newId').length 应返回 null。在 Firebug 控制台中测试

用途:

clone.attr('id', 'newId') 

在克隆对象的 DOM 元素上设置 ID。

关于Jquery 拖放和克隆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2100896/

相关文章:

php - 如何从 AJAX 以 JSON 形式返回数组并读取结果

javascript for 循环不运行所有代码

c# - 从文件夹中拖放文件 c# wpf

cocoa - 奇怪的行为: dragging from Stacks to status item doesn't work

java - 实现拖放到图像查看器中

javascript - 如何根据 div 元素的编号在 div 元素内创建新的 bullent 图标?

jquery - 使用链接打开 jqueryUI 模式

javascript - 知道延迟加载图像后放置的 div 的偏移量

java - 如何在 Swing 中使用拖放来获取文件路径?

json - 在Mac Catalyst中运行的iOS应用程序中的DropInteraction中接收JSON文件