Jquery拖放和克隆

Saj*_*eev 6 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");



               }
        });



    });
Run Code Online (Sandbox Code Playgroud)

fre*_*rik 3

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

使用:

clone.attr('id', 'newId') 
Run Code Online (Sandbox Code Playgroud)

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