2012-11-18 167 views
0

我搞砸了一些代码,我有一个列表,我已经应用可拖动功能,另一个div与拖放功能。删除列表项后删除它

我想要实现的是当列表元素被删除时,它将从父列表中删除并添加到另一个列表中。

这里是我与打码:

<div id='dAvail'> 
<ul> 
    <li><div class='abox'>1</div></li> 
    <li><div class='abox'>2</div></li> 
</ul> 
</div> 
<div id='somewhereToDrop'> 
</div> 
<div id='newListHere'> 
<ul> 
</ul> 
</div> 

的JS:

$(document).ready(function(){ 
//add the droppable function to the divs in main list 
$('#dAvail ul).each(function(){ 
    $('li>div).draggable(); 
}); 

$('#somewhereToDrop').droppable({ 
    tolerance:'touch', 
    drop:function(event,ui){ 
    //this is where I am having problems. 
    //if I do ui.draggable I get an object representing the object I dragged but not 100% how to remove said object from the orginal list to then append to the new one. 
    //this is what i tried. 
    var t = ui.draggable[0]; //represents the div that i dropped. 
    $(t).parent().parent().remove($(t).parent()); 
    //that is as far as I have gotten as this returns an error 


}); 

如果有人能指出如何去从列表中删除该对象将是巨大的。 我甚至试图看看我是否可以做$(t).parent()。eq()但没有用,这个想法是得到列表索引然后像这样删除它。

感谢

回答

1

remove()并不需要的参数。它从DOM中删除它的实例。所以,真的,所有你想要做的是:

$(t).parent().remove(); 
+0

谢谢你。我想到,我实际上不想调用remove,只是将它附加到新列表中,但是这帮助我解决了如何到达那里。 – Vade

+0

如果您不想完全删除,请尝试使用.detach()。它会返回对象,并将其保存在内存中,但将其从DOM中删除。这可以让你稍后重新插入一个你想要的。 –