无法在初始化之前调用 draggable 上的方法
cannot call methods on draggable prior to initialization
我正在尝试销毁 jQuery UI 的可拖动实例,但我收到 'cannot call method' 错误。
我的代码
$('table.paper tr').draggable({
helper: 'clone',
create: function(event, ui) {
$('body').on('click', '[data-action="edit-ingredients"]', function(event) {
event.preventDefault();
$('table.paper').draggable('destroy');
});
},
start: function(event, ui) {
c.tr = this;
c.helper = ui.helper
$(this).hide();
},
drag: function(event, ui) {
var collides = $('table.paper').overlaps($(c.helper));
if (collides.hits.length) {
$(c.helper).removeClass('delete');
} else {
$(c.helper).addClass('delete');
}
}
});
错误
Error: cannot call methods on draggable prior to initialization; attempted to call method 'destroy'
http://code.jquery.com/jquery-1.11.0.min.js
Line 2
在 'logic speak' 中,此错误告诉我我的代码流不正确,因为 jQuery UI 的可拖动对象在我尝试销毁它时未启动 -但是,如您所见,我正在可拖动对象的 'create' 事件中创建事件侦听器。
您正在使用选择器 $('table.paper tr')
实例化可拖动对象,然后使用不同的选择器 $('table.paper')
销毁可拖动对象,我认为这不是可拖动元素。
有时,了解元素是否可拖动的最佳解决方案之一是检查是否存在名为 .ui-draggable
的 class。
我正在尝试销毁 jQuery UI 的可拖动实例,但我收到 'cannot call method' 错误。
我的代码
$('table.paper tr').draggable({
helper: 'clone',
create: function(event, ui) {
$('body').on('click', '[data-action="edit-ingredients"]', function(event) {
event.preventDefault();
$('table.paper').draggable('destroy');
});
},
start: function(event, ui) {
c.tr = this;
c.helper = ui.helper
$(this).hide();
},
drag: function(event, ui) {
var collides = $('table.paper').overlaps($(c.helper));
if (collides.hits.length) {
$(c.helper).removeClass('delete');
} else {
$(c.helper).addClass('delete');
}
}
});
错误
Error: cannot call methods on draggable prior to initialization; attempted to call method 'destroy'
http://code.jquery.com/jquery-1.11.0.min.js
Line 2
在 'logic speak' 中,此错误告诉我我的代码流不正确,因为 jQuery UI 的可拖动对象在我尝试销毁它时未启动 -但是,如您所见,我正在可拖动对象的 'create' 事件中创建事件侦听器。
您正在使用选择器 $('table.paper tr')
实例化可拖动对象,然后使用不同的选择器 $('table.paper')
销毁可拖动对象,我认为这不是可拖动元素。
有时,了解元素是否可拖动的最佳解决方案之一是检查是否存在名为 .ui-draggable
的 class。