jQuery删除一个对象并替换在同一个地方

编程入门 行业动态 更新时间:2024-10-26 08:22:13
本文介绍了jQuery删除一个对象并替换在同一个地方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何从无序列表中删除对象并将其添加到另一个列表中,同时保留其位置?我知道我可以将原始项目设置为hide()但是然后会留下项目用来显示的小空间。假设我有一个列表A和B,如果用户点击A中的项目,则需要将其添加到B并从A中删除。如果他们现在单击按钮将其从B中删除并返回到A,我想要让它显示在原来的相同位置。我知道我可以通过append()添加它但不会将它放在同一个位置。有人想过吗?

How would I go about removing an object say from an unordered list and add it to another list while keeping its location? I know i can set the original item to hide() but then that leaves a small space where the item use to appear. Say i have a list A and B, if a user clicks on an item in A it needs to be added to B and removed from A. If they now click on a button to remove it from B and back to A, i want to have it displayed in the same spot it originally did. I know i can add it via append() but that won't put it in the same location. Thoughts anyone?

推荐答案

你可以使用.clone吗?所以:

You could use .clone? so:

<ul id="firstlist"> <li>One</li> <li>Two</li> <li>Three</li> <li>Four</li> </ul> <ul id="secondlist"> <li>One</li> <li>Two</li> <li>Three</li> </ul>

jquery:

var object = $('#firstlist > li :last ').clone(); $('#secondlist').append(object); $('#firstlist > li :last ').destroy();

显然你可以交换这一轮将它添加到第一个列表,但要获得你能够的位置我可能会采取多种方式:

Obviously you can swap this round to add it to the first list, but to get the position you could use a number of ways, I would probably do:

$('#firstlist').children('li').each(function(i){ });

并使用i获取头寸。您可以在给定对象之后追加:

and use "i" to get the position. you can append after a given object to so:

$('#firstlist').children('li').each(function(i) { if(i== 4){ $(this).append(object).after(this) }

更多推荐

jQuery删除一个对象并替换在同一个地方

本文发布于:2023-11-22 08:31:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616697.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   地方   在同一个   jQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!