简体   繁体   English

如何使用jQuery从UL中添加和删除LI?

[英]How to add and remove a LI from UL with jQuery?

How do i use jQuery to add and remove item from unordered lists? 我如何使用jQuery从无序列表中添加和删除项目? When i doubleclick on an item in list 1 it should be removed from list 1 and added to list 2 And of course, the other way around as well... 当我双击列表1中的项目时,应将其从列表1中删除并添加到列表2中。当然,反之亦然...

I've got the following lists: 我有以下列表:

<ul id='attached'>
  <li id='itemID_1' ondblclick='removeAttached(&#39;itemID_1&#39;)'>Item</li>
  <li id='itemID_2' ondblclick='removeAttached(&#39;itemID_2&#39;)'>Item</li>
  <li id='itemID_3' ondblclick='removeAttached(&#39;itemID_3&#39;)'>Item</li>
  <li id='itemID_4' ondblclick='removeAttached(&#39;itemID_4&#39;)'>Item</li>
</ul>

<ul id='non-attached'>
  <li id='itemID_5' ondblclick='addAttached(&#39;itemID_5&#39;)'>Item</li>
  <li id='itemID_6' ondblclick='addAttached(&#39;itemID_6&#39;)'>Item</li>
  <li id='itemID_7' ondblclick='addAttached(&#39;itemID_7&#39;)'>Item</li>
  <li id='itemID_8' ondblclick='addAttached(&#39;itemID_8&#39;)'>Item</li>
</ul>

I was thinking something like: 我在想类似的东西:

<script type='text/javascript'>")
  function addAttached(i) { $('#non-attached').remove(i); $('#attached').append(i); };")
  function removeAttached(i) { $('#attached').remove(i); $('#non-attached').append(i); };")
</script>")

But i might be pretty off here? 但是我可能在这里很漂亮?

If you want to move things back and forth, your best bet is event delegation : 如果您想来回移动,最好的选择是事件委托

<ul id='attached'>
    <li id='itemID_1'>Item</li>
    <li id='itemID_2'>Item</li>
    <li id='itemID_3'>Item</li>
    <li id='itemID_4'>Item</li>
</ul>

<ul id='non-attached'>
    <li id='itemID_5'>Item</li>
    <li id='itemID_6'>Item</li>
    <li id='itemID_7'>Item</li>
    <li id='itemID_8'>Item</li>
</ul>

JS JS

$("#attached").delegate("li", "dblclick", function() {
    $("#non-attached").append(this);
});

$("#non-attached").delegate("li", "dblclick", function() {
    $("#attached").append(this);
});

This will detect a click on an li element which bubbles up to your list. 这将检测到对li元素的单击,该单击会冒泡到您的列表。 Then it will move the element to the other list. 然后它将元素移动到另一个列表。

JSFiddle: http://jsfiddle.net/TYwPU/ JSFiddle: http : //jsfiddle.net/TYwPU/

You don't need to remove the element, you can just call appendTo : 您不需要remove该元素,只需调用appendTo

$("#attached li").dblclick(function() {
    $(this).appendTo("#non-attached");
});
$("#non-attached li").dblclick(function() {
    $(this).appendTo("#attached");
});

Here's a working example . 这是一个有效的例子 Note that the above code should be placed in a ready event handler, and it removes the need for inline event handlers. 请注意,以上代码应放在ready事件处理程序中,并且不需要内联事件处理程序。

Update based on comments 根据评论更新

Because the element is removed from the DOM and reattached somewhere else, it loses the event handler that was bound to it. 因为该元素已从DOM中删除并重新附加到其他位置,所以它将丢失绑定到它的事件处理程序。 That means you need to use the jQuery live or delegate methods, which bind event handlers to elements matching the selector now or in the future: 这意味着您需要使用jQuery livedelegate方法,该方法将事件处理程序绑定到现在或将来与选择器匹配的元素:

$("#attached li").live("dblclick", function() {
    $(this).appendTo("#non-attached");
});
$("#non-attached li").live("dblclick", function() {
    $(this).appendTo("#attached");
});

First of all, you should avoid writing inline javascript. 首先,您应该避免编写内联javascript。 It makes it harder to debug and maintain your codebase. 这使得调试和维护您的代码库变得更加困难。 I recommend that you read the following articles on javascript events: 我建议您阅读以下有关javascript事件的文章:

Quirksmode - Early Event Handlers Quirksmode-早期事件处理程序

Quirksmode - Traditional event registration model Quirksmode-传统事件注册模型

This type of event handling should not be mixed with jQuery. 此类事件处理不应与jQuery混合使用。 Follow @james-allardice example and try learning how javacript should be used correctly. 按照@ james-allardice示例,尝试学习如何正确使用javacript。 I recommend that you read the jQuery docs and tutorials here . 我建议您在这里阅读jQuery文档和教程。

Also, if you are in a hurry you could try the more appealing JQuery UI Sortable lists . 此外,如果您急于赶快,可以尝试使用更具吸引力的JQuery UI Sortable list

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM