2

我想在刪除容器上的<div>時刪除它的拖動屬性。但是我得到了一個錯誤"Property 'draggable' of object #<Object> is not a function",我的代碼如下。ui.draggable(「destroy」)的jquery ui錯誤

$("#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

+1

不相關的問題,但你可能要考慮當你有這麼多的時候使用一個類作爲可拖動的。例如:'$(「.draggable」).draggable();' – mekwall 2012-04-05 08:22:42

回答

0

我的猜測是, 'UI' 是一個簡單的JavaScript老對象,而不是一個jQuery對象。
嘗試(修訂本):
$(ui.draggable).draggable("destroy");

+0

但是當我嘗試獲取對象的id時,它給了我null。 – northlondoner 2012-04-05 10:34:39

+0

啊,我的jquery-ui看起來很生鏽,根據jquery-ui的演示: 「$(this)表示可拖動的可拖動的元素,ui.draggable表示可拖動的元素。」參見修訂版。 – 2012-04-05 14:30:38

0

的語法調用methods of draggable widget是:

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

你應該通過方法的名稱爲stringdraggable()方法。

在drop事件回調中,ui.draggable只是對與可拖動元素對應的jQuery對象(語法的$(".selector")部分)的引用。

你應該實際調用它draggable(),並通過該方法名稱:

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

我使用setTimeout函數解決了這個問題:

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