jQuery droppable out 事件无法触发?

发布于 2024-08-05 20:51:01 字数 743 浏览 8 评论 0原文

$(".LWdrop").droppable({    
    accept: ".LW",                       
    drop: function(event, ui){
      ui.draggable.addClass("LWactive");
      $(this).droppable('option', 'accept','');
      $(this).css("background-color", "#444444");
    },
    out: function(event, ui){  
      $(this).droppable('option', 'accept', '.LW');
      ui.draggable.removeClass("LWactive");
    },   
    activate: function(event, ui){     
      $(this).css("background-color", "blue");      
    },  
    deactivate: function(event, ui){  
      $(this).css("background-color", "#444444"); 
    }
});

请忽略激活/停用时丑陋的背景颜色变化,这仅用于测试。我遇到了一个问题,即“out”没有被触发。这是否与可拖动对象设置为“恢复:无效”有关?即使删除它,我也无法从 out 事件中获取任何内容来执行......甚至是一个简单的警报框。有什么建议吗?

$(".LWdrop").droppable({    
    accept: ".LW",                       
    drop: function(event, ui){
      ui.draggable.addClass("LWactive");
      $(this).droppable('option', 'accept','');
      $(this).css("background-color", "#444444");
    },
    out: function(event, ui){  
      $(this).droppable('option', 'accept', '.LW');
      ui.draggable.removeClass("LWactive");
    },   
    activate: function(event, ui){     
      $(this).css("background-color", "blue");      
    },  
    deactivate: function(event, ui){  
      $(this).css("background-color", "#444444"); 
    }
});

Please ignore the ugly background-color changes on activate/deactivate, that's only for testing. I'm having a problem where "out" isn't being triggered. Could this have to do with the fact that the draggables are set to "revert: invalid"? Even removing that, I fail to get anything from the out event to execute...even a simple alert box. Any tips?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

來不及說愛妳 2024-08-12 20:51:01

当您将 draggable 悬停在 droppable 上然后将其移开时,out 事件就会被触发。不幸的是,当您将 draggable 拖走时,情况并非如此。

The event out is triggerd when you hover a draggable over a droppable and then move it away. Unfortunately not when you drag a draggable away.

野侃 2024-08-12 20:51:01

当我试图让助手在拖动到可放置对象上时在视觉上发生变化(通过切换类)时,我遇到了类似的问题。和你一样,我发现 out 事件没有在我预期的时候触发。

通过一些试验和错误,我通过在结束事件上添加类并在停用事件上删除它来解决了问题。

$("#droppable").droppable({
    over: function(event, ui) {
        ui.helper.toggleClass("over", true);
    },
    deactivate: function(event, ui) {
        ui.helper.toggleClass("over", false);
    }
});

I ran into a similar issue when trying to make the helper to change visually (by toggling a class) when dragged over a droppable. Like you, I found the out event didn't fire when I was expecting it to.

Through some trial and error, I solved the problem by adding the class on the over event, and removing it on the deactivate event.

$("#droppable").droppable({
    over: function(event, ui) {
        ui.helper.toggleClass("over", true);
    },
    deactivate: function(event, ui) {
        ui.helper.toggleClass("over", false);
    }
});
终止放荡 2024-08-12 20:51:01

这对某人可能有帮助。

$("#element").droppable({
    over: function( event, ui ) { // dragged over #element
        $(event.target).removeClass('out');
        $(event.target).addClass('over');
    },          
    out: function( event, ui ) { // dragged out #element
        $(event.target).removeClass('over');
        $(event.target).addClass('out');
    },
    deactivate: function (event, ui) { // dropped somewhere
        state = $(event.target).hasClass('over') ? 'over' : 'out');
        if (state == 'out') {
            alert('dropped outside #element');
        }
    }
});

This can be helpful for someone.

$("#element").droppable({
    over: function( event, ui ) { // dragged over #element
        $(event.target).removeClass('out');
        $(event.target).addClass('over');
    },          
    out: function( event, ui ) { // dragged out #element
        $(event.target).removeClass('over');
        $(event.target).addClass('out');
    },
    deactivate: function (event, ui) { // dropped somewhere
        state = $(event.target).hasClass('over') ? 'over' : 'out');
        if (state == 'out') {
            alert('dropped outside #element');
        }
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文