2

大家好,我有一个功能,我必须在这里拖放元素,这是我的代码:

$(document).ready(function() {

    var i = 0;
    $("button[id='columnadd']").click(function () {
        alert(1);
            var domElement = $('<aside id="shoppingCart' + i + '" class="shoppingCart"><h2 class="ui-widget-header">Add Menu Items Here</h2><aside class="ui-widget-content" id="droppable"><ol><li class="placeholder">Add your items here</li></ol></aside></aside>');
            i++;
            $(this).after(domElement);
    });

        $(".small_box li" ).draggable({
            appendTo: "body",
            helper: "clone"
        });
});

$('#shoppingCart ol').droppable({
    accept: '.small_box li',
    drop: function (event, ui) {
        alert(1);
        $(this).find(".placeholder").remove();
        $("<li></li>").text(ui.draggable.text()).appendTo(this);
    }
});

这是我的html:

    <aside class="small_box">
    <h4>BRANDS</h4>
    <ul>
        <li id ="brand1"><a class="" href="#">Brand1</a></li>
        <li id ="brand2"><a href="#">Brand2</a></li>
        <li id ="brand3"><a href="#">Brand3</a></li>
        <li id ="brand4"><a href="#">Brand4</a></li>
     </ul>
    </aside>

我应该可以来这个地方<li class="placeholder">Add your items here</li>

var domElement = $('<aside id="shoppingCart' + i + '" class="shoppingCart"><h2 class="ui-widget-header">Add Menu Items Here</h2><aside class="ui-widget-content" id="droppable"><ol><li class="placeholder">Add your items here</li></ol></aside></aside>');

任何帮助深表感谢

4

2 回答 2

0

首先,请修复您的代码缩进。

您的代码有一些错误:

  1. var i = 0; 我认为它应该是一个全局变量。所以把它放在你的$(document).ready(function() {})街区之外;

  2. 您在单击时创建一个 DOM 元素(这是放置区域),并且在您单击触发器之前,该元素不在您的页面中。通过$('#shoppingCart ol').droppable({...})$(document).ready(function() {})块中执行,您尝试添加droppable一个甚至不在您的页面中的元素,所以这不会做任何事情。

您应该做的是在创建该元素并将其放在页面上droppable 之后制作该元素。这是一个例子:

$("button[id='columnadd']").click(function () {
  // create the element
  var domElement = $('<aside id="shoppingCart' + i++ + '" class="shoppingCart"><h2 class="ui-widget-header">Add Menu Items Here</h2><aside class="ui-widget-content" id="droppable"><ol><li class="placeholder">Add your items here</li></ol></aside></aside>');

  // put it after $(this)
  $(this).after(domElement);
  // at this point you have domElement in your page

  // make it droppable
  domElement.find("ol").droppable({
    // put options here
    greedy: true,
    drop: function (event, ui) {
      makeItDroppableToo(event, ui, this);
    }
  });
});

这是使您删除的元素具有自己的占位符的功能

function makeItDroppableToo(e, ui, obj) {
  $(obj).find(".placeholder").remove();

  var placeholder = $("<ol><li class='placeholder'>You can also drop it here</li></ol>");
  $("<li></li>").append(ui.draggable.text()).append(placeholder).appendTo($(obj));

  // this is the same as the previous one
  placeholder.droppable({
    // put options here
    greedy: true,
    drop: function (event, ui) {
      makeItDroppableToo(event, ui, this);
    }
  });
}
于 2012-10-29T06:26:55.220 回答
0

试试这个小提琴,我正在使用 JQueryUI

http://jsfiddle.net/ashpriom/yHLGP/

  $(function() {
$( "#draggable" ).draggable();
$( "#droppable" ).droppable({
  drop: function( event, ui ) {
    $( this )
      .addClass( "ui-state-highlight" )
      .find( "p" )
        .html( "Dropped!" );
  }
});

});

于 2013-09-03T09:21:21.043 回答