jquery ui error for ui.draggable("destroy")

jquery ui error for ui.draggable("destroy")

本文关键字:quot ui destroy error for draggable jquery      更新时间:2023-09-26

当我把<div>放在容器上时,我想删除它的拖动属性。但是对于下面的代码,我得到了一个错误"Property 'draggable' of object #<Object> is not a function"

$( "#fighter1" ).draggable(); //fighter1 is the id of draggable object
$( "#fighter2" ).draggable();
$( "#fighter3" ).draggable();
$( "#fighter4" ).draggable();
$( "#fighter5" ).draggable();
$( "#fighter6" ).draggable();
$( "#dest" ).droppable({      //dest is the id of droppable object
   drop: function( event, ui ) {
      ui.draggable("destroy"); //I get error here.
   }
});

我使用jquery ui 1.8.12版本

我的猜测是"ui"是一个简单的旧javascript对象,而不是jQuery对象
尝试(修订):
$(ui.draggable).draggable("destroy");

调用可拖动小部件方法的语法为:

$( ".selector" ).draggable( "method" );

您应该将方法名称作为string传递给draggable()方法。

在drop事件回调中,ui.draggable只是对可拖动元素(语法的$( ".selector" )部分)对应的jQuery对象的引用。

实际上,您应该调用它上的draggable()并传递方法名:

ui.draggable.draggable("destroy");
----^-------        ------^------
selector                method name
       --------^--------
   this guy executes the method

我使用setTimeout函数解决了这个问题:

setTimeout(function(a){a.draggable("destroy");},100,ui.draggable);