-1

我希望从几个连接的 ul 列表中动态删除 li 元素。现在我正在通过使用分配“dblclick”事件行为$("#sortable1").children().on('dblclick',function() {...})

#sortable1 中的项目将被用户移动到其他列表(#sortable2、#sortable3 等)。

双击列表项时,会弹出一个对话框,询问用户是否要删除它。如果用户说是,我希望将列表项从它所在的任何列表中删除。我正在尝试使用类似的方法来做到这一点:

$($(this).parent().childNodes[$(this).index()]).remove()

但这不起作用。

建议?

4

2 回答 2

0

像这样的东西应该工作。

var showPopup = function( elem ){
  //show your popup with a function like this, as i assume it already does...      
  $( '#delete_toggle' ).one( 'click', function(){
    elem.remove();
  });
};

$("#sortable1").children().on('dblclick',function(){
  showPopup( $(this) );
});
于 2012-07-27T20:09:20.110 回答
0

To remove the element that was clicked on, you just use this in the event handler:

$(this).remove();

or if you've saved the element reference to a variable elem, it would be this:

$(elem).remove();

You are trying to make it way more complicated than need be. The jQuery .remove() method looks at who its current parent is and takes care of all that for you.

于 2012-07-27T20:24:46.287 回答