【问题标题】:jQuery Droppable, get the element droppedjQuery Droppable,让元素被丢弃
【发布时间】:2011-02-27 20:09:32
【问题描述】:

一个小问题,希望有一个简单的答案,我正在使用 jQuery 可拖动和可拖放将项目放置到停靠栏中。使用下面的代码进行放置。

$("#dock").droppable({
            drop: function(event, ui) {
                //Do something to the element dropped?!?
            }
        });

但是我找不到获取实际删除的元素的方法,所以我可以做点什么。这可能吗?

【问题讨论】:

    标签: jquery jquery-ui jquery-ui-draggable jquery-ui-droppable


    【解决方案1】:

    来自drop event documentation

    当一个 接受的可拖动被丢弃'over' (在公差范围内)这个 可丢弃。在回调中,$(this) 代表 droppable 和 draggable 掉在上面。而ui.draggable 代表 可拖动的。

    所以:

    $("#dock").droppable({
         drop: function(event, ui) {
                   // do something with the dock
                   $(this).doSomething();
    
                   // do something with the draggable item
                   $(ui.draggable).doSomething();
               }
    });
    

    【讨论】:

    • 是否需要移除活动的css类
    • 对我来说,这使元素具有拖动状态,位置:相对和左/右坐标
    • 在 doSomething() 函数中究竟如何选择可拖动对象?
    • 此代码有效。 $("#drag").draggable(); $("#drop").droppable({ drop: function (event, ui) { $(this).css("background-color","red"); } }); $("#drop").droppable({ out: function( event, ui ) { $(this).css("background-color","yellow"); } }); 为什么这段代码不起作用? $("#drag").draggable(); $("#drop").droppable({ drop: function (event, ui) { $(this).css("background-color","red"); } out: function( event, ui ) { $(this).css("background-color","yellow"); } });codepen
    • 我不得不使用 ui.draggable[0] 。 ui.draggable 是一个数组。
    猜你喜欢
    • 1970-01-01
    • 2015-05-23
    • 1970-01-01
    • 1970-01-01
    • 2016-04-08
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多