删除后,从列表中删除列表项

时间:2012-11-18 13:59:04

标签: jquery html jquery-ui

我正在搞乱一些代码,我有一个列表,我已经应用了draggable函数,另一个div有一个drop函数。

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

这是我正在玩的代码:

    <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 个答案:

答案 0 :(得分:1)

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

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