0

我正在研究使用 jquery ui 可拖放/可拖放的概念证明。

我正在寻找一种方法来设置一个变量,该变量等于放入可放置区域的 div 的索引。(假设我有名为“div0”、“div1”、“div2”等的 div……)

所以想象一下代码看起来像这样:

<div id="div0" class="draggableDiv">some cotent</div>
<div id="div1" class="draggableDiv">more content</div>
<div id="div2" class="draggableDiv">yet more content</div>

<div class="droppableDiv">drop it here</div>

$(document).ready(function() {
    $('.draggableDiv').draggable({helper: 'clone'});
    $('.droppableDiv').droppable({
        accept: '.draggableRecipe',
        drop: function(ev, ui) { 
            ui.draggable.clone().fadeOut("fast", 
            function() { 
                $(this).fadeIn("fast") 
            }).appendTo($(this).empty());
                        // function to set someVar = div[index];
        }
    });
});

我不确定如何最好地做到这一点,所以如果有人有任何建议,我会很感激一些指导。

谢谢!

4

3 回答 3

2
<div id="div0" class="draggableDiv">some cotent</div>
<div id="div1" class="draggableDiv">more content</div>
<div id="div2" class="draggableDiv">yet more content</div>

<div id='dropplace' class="droppableDiv">drop it here</div>

<script language='javascript'>

$(document).ready(function() {
    $('.draggableDiv').draggable({helper: 'clone'});
    $('.droppableDiv').droppable({
        accept: '.draggableDiv', //changed this, it was originally 'draggableRecipe'
        drop: function(ev, ui) { 

                //get the index of dropped draggable div
                var divIndex = $('div').index(ui.draggable)
                alert(divIndex) //alert the divIndex to see the result

                ui.draggable.clone().fadeOut("fast", 
                function() { 
                        $(this).fadeIn("fast") 
                }).appendTo($(this).empty());
        }
    });
});
</script>

测试了这个,它对我有用,希望对你也有用。祝你好运

于 2009-02-06T06:45:31.927 回答
0

在行后 drop: function(ev, ui) { 添加:

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

之后droppedId将是您的div0,div1div2- 之一,具体取决于哪个被丢弃。

于 2011-04-08T04:46:13.567 回答
0

您应该能够使用解析 div 的 id

$.droppedItems = {};

var droppedId = $(this).attr('id').replace('droppable', '');
$.droppedItems[droppedId] = ui.draggable.attr('id').replace('div', '');
于 2009-02-05T18:18:19.593 回答