【问题标题】:Remove item on drag out JQuery在拖出 JQuery 时删除项目
【发布时间】:2020-07-30 15:34:53
【问题描述】:

我想启用从 droppable 中退出的选项我确实找到了我需要的 here,但是我正在使用其他库然后他/我想将内衬分析器“翻译”到我所在的库使用。我正在使用this library

接受答案的代码:

$(".draggable").draggable({
    cursor: "move",
    revert: "invalid",
    helper: "clone"
});

$(".droppable").droppable({
    accept: '.draggable',
    hoverClass: "ui-state-active",
    drop: function (ev, ui) {
        if ($(ui.draggable).hasClass('new')) {
            $('.new').draggable({
                revert: true
            });
        } else {
            $(this).append($(ui.draggable).clone().draggable({
                helper: "original"
            }).addClass('new'));
        }
    },
    out: function (event, ui) {
        $(ui.draggable).fadeOut(1000, function () {
            $(this).remove();
        });
    }

但我需要类似的东西

droppable.on('drag:start', () => {
   $('.source-point').addClass('show');
//...
});
droppable.on('drag:stop', (evt) => {
   $('.source-point').removeClass('show');
//...
});

【问题讨论】:

    标签: jquery draggable


    【解决方案1】:

    试试这个:

    my answer to other question, see example.的修改版

    var i = 0;
    var removeClass="";
    
    droppable.on('drag:start', () => {
       $('.source-point').addClass('show');
    });
    droppable.on('drag:stop', (evt) => {
       $('.source-point').removeClass('show');
     document.querySelector("."+removeClass).remove();
    });
    
    
    [...document.querySelectorAll('body *')].forEach(el => {
      el.addEventListener('mousedown', event => {
        el.classList.add("clicked"+(++i));
        console.clear();    
        removeClass="clicked"+(+i);
      })
    })
    

    【讨论】:

    • 为什么要混合使用 jQuery 选择器和document.querySelector
    • @SeanKendle 我的代码是纯 JS 并且我已经编写好了,我只是将一行复制到给定的代码中。我为什么不呢?
    • 在同一代码中从 $(".className) 转到 document.querySelector(".className") 似乎很奇怪。这是一个一致性的事情。
    • @SeanKendle 那是医学强迫症,不是编码问题 :))
    • 那么我不想读你的代码。一致性在编码中非常重要。
    猜你喜欢
    • 2013-05-06
    • 1970-01-01
    • 1970-01-01
    • 2017-04-06
    • 2020-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多