jquery droppable only one child

ε祈祈猫儿з 提交于 2019-12-14 02:40:34

问题


I am new to jQuery and I am working with the droppable API.

I want to have a group of divs that can all hold one and only one droppable item. I've set the class of my droppable divs to inv. I can drop items into the divs but I can find a way to reject a drop once in the drop function.

I want to be able to detect that my div already has a child and if it does revert the dopped element.

my code currently looks like this

$( "div.inv" ).droppable(
{
    drop: function( event, ui ) 
    {
        childCount = $(this).children().length;
        if (childCount !=0)
        {
            //revert droppable to initial position
            return;
        }   
          //if there is a child revert and return
         $( this )
            .addClass( "ui-state-highlight" )
            .append($(ui.draggable))
    }
});

回答1:


What about disabling the droppable area after receiving an item ?

You can do something like this :

$( "div.inv" ).droppable(
{
    drop: function( event, ui )  {
        $(this).droppable('disable');
    }
});


来源:https://stackoverflow.com/questions/5145747/jquery-droppable-only-one-child

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!