jquery 将元素移动到最后一个子位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18427255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:37:11  来源:igfitidea点击:

jquery move element to last child position

jqueryhtmldom

提问by Omid

Let's say I have a <div>that contains some random elements, but there is one element I've created with this unique selector : $('.myUniqueElement'). I want to use .insertAfter()to move this element at last child position in my <div>is that possible ? what is the solution ?

假设我有一个<div>包含一些随机元素的元素,但是我使用这个独特的选择器创建了一个元素:$('.myUniqueElement')。我想用来.insertAfter()在我的最后一个子位置移动这个元素,<div>这可能吗?解决办法是什么 ?

This is my solution: http://jsfiddle.net/silverlight/RmB5K/3/is this OK ??

这是我的解决方案:http: //jsfiddle.net/silverlight/RmB5K/3/可以吗??

HTML:

HTML:

<div class='p'>
    <div class='myUniqueElement'>unique</div>
    <div>item</div>
    <div>item</div>
    <div>item</div>
</div>
<input type='button' id='b' value='Do'/>

Javascript:

Javascript:

$('#b').on('click',function(){
    $('.myUniqueElement').insertAfter($('.p :last-child'))
});

CSS:

CSS:

.myUniqueElement{color:red;}

回答by Arun P Johny

Try

尝试

var mydiv = $('my-div-selector');
mydiv.find('.myUniqueElement').appendTo(mydiv)

or simple

或简单

$('my-div').append($('.myUniqueElement'))

回答by Alnitak

There is no need to use .insertAfter. You should also notuse .cloneas it'll remove any attached data or events, unless you use .clone(true).

没有必要使用.insertAfter. 你也应该使用.clone,因为它会删除所有已连接的数据或事件,除非你使用.clone(true)

Simply use .appendToto add the target element to the divand it'll get detached from its original position and added as the last child of the div:

只需使用.appendTo将目标元素添加到div,它就会从其原始位置分离并添加为 的最后一个子元素div

$('.myUniqueElement').appendTo('#my-div');

回答by Omid

This is my solution and I think this is the best way I can use .insertAfter():

这是我的解决方案,我认为这是我可以使用的最佳方式.insertAfter()

HTML:

HTML:

<div class='p'>
    <div class='myUniqueElement'>unique</div>
    <div>item</div>
    <div>item</div>
    <div>item</div>
</div>
<input type='button' id='b' value='Do'/>

jquery:

查询:

$('#b').on('click',function(){
    $('.myUniqueElement').insertAfter($('.p :last-child'))
});

The point is :last-childand this is the fiddle: http://jsfiddle.net/silverlight/RmB5K/3/

关键是:last-child,这是小提琴:http: //jsfiddle.net/silverlight/RmB5K/3/

回答by Fabio Caccamo

There is no need to call $ more than once, just move the element to the end of its parent:

不需要多次调用 $ ,只需将元素移动到其父元素的末尾即可:

var myEl = $('.my-el-selector');
myEl.appendTo(myEl.parent());

回答by vikas devde

In order to use insertAfter(), you must know the element after which you want to move your element to. And as you said you have random elements, and of course you don't know the current last element, so here what you can do.

为了使用insertAfter(),您必须知道要将元素移动到的元素。正如你所说,你有随机元素,当然你不知道当前的最后一个元素,所以在这里你可以做什么。

Clone your element that you want to move and append it to parent element.

克隆要移动的元素并将其附加到父元素。

The jQuery code looks like this:

jQuery 代码如下所示:

$clone_elem = $('.myUniqueElement').clone();
$('.myUniqueElement').remove();
$('div').append($clone_elem);

Checkout this fiddle

结帐这个小提琴

回答by Yudho Ahmad Diponegoro

Vikas is right. However, probably this one is an improved version of it since the original element is removed, not its clone....

维卡斯是对的。然而,这可能是它的改进版本,因为原始元素被删除,而不是它的克隆......

    $elem = $('.myUniqueElement')
    $('div').append($elem.clone());
    $elem.remove();