如何从无序列表中删除对象并将其添加到另一个列表中,同时保留其位置?我知道我可以将原始项目设置为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删除一个对象并替换在同一个地方
发布评论