Jquery Drag and;丢弃:将元素从li更改为div并返回

时间:2013-04-05 08:58:48

标签: javascript jquery jquery-ui draggable droppable

我正在尝试以下方法:

第1阶段:

  1. #canvas
  2. 中将可拖动元素从 li 更改为 div
  3. 将可拖动元素移动到 #canvas
  4. 第二阶段:

    1. #imagelist
    2. 中将可拖动元素从 div 更改为 li
    3. 将可拖动元素移到 #imagelist
    4. JS:

      $(function () {
          var $imagelist = $("#imagelist");
          var $canvas = $("#canvas");
      
          $('li', $imagelist).draggable();
      
          $canvas.droppable({
              drop: function (event, ui) {}
          });
      
          $imagelist.droppable({
              drop: function (event, ui) {}
          });
      });
      

      HTML:

      <div id="listwrapper">
        <ul id="imagelist">
           <li class="draggable>Content that should not be lost on drag/drop</li>
           <li class=" draggable>Content that should not be lost on drag/drop</li>
        </ul>
      </div>
      <div id="canvas">
        <div class="draggable">Content that should not be lost on drag/drop</div>
      </div>
      

      有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:0)

您可以创建一个新函数来更改元素类型 - 这是我认为您需要的:

(function($) {
    $.fn.changeElementType = function(newType) {
        var attrs = {};

        $.each(this[0].attributes, function(idx, attr) {
            attrs[attr.nodeName] = attr.nodeValue;
        });

        this.replaceWith(function() {
            return $("<" + newType + "/>", attrs).append($(this).contents());
        });
    };
})(jQuery);

see here

and here