Uncaught Error: cannot call methods on draggable prior to initialization; attempted to call method 'disable'

Uncaught Error: cannot call methods on draggable prior to initialization; attempted to call method 'disable'

https://jsfiddle.net/scottbeeson/r5du4p6k/12/

如果长按拖动,松开时出现以下错误:

Uncaught Error: cannot call methods on draggable prior to initialization; attempted to call method 'disable'

var t;
$(document).on('touchstart mousedown','.menu-item', function (event) {
    var self = this;
    if ($(self).hasClass('draggable')) return;
    t = setTimeout(function () {
        $(self).draggable({
            revert: true,
            helper: 'clone',
            opacity: .75,
            appendTo: 'body'
        }).draggable('enable').addClass('draggable');
        $(self).trigger(event)
    }, 800);
});

$(document).on("touchend mouseup", function () {
    clearTimeout(t);
    $('.draggable').draggable( 'disable' ).removeClass('draggable');
});

由于您复制了原始元素,因此当您尝试删除 draggable - 您也会对刚刚复制的元素(不可拖动)执行此操作,因此您不能 disable它。

您可以做的是仅在第一个元素上禁用它:

$('.draggable').first().draggable( 'disable' ).removeClass('draggable');

检查这个:
https://jsfiddle.net/r5du4p6k/13/