在javascript中移动n:th之后的元素

时间:2013-08-28 11:58:38

标签: jquery

我正在尝试使用类stamp2移动元素,因此它位于第五个子元素之后。这就是我的尝试。该项似乎已删除。

$('.stamp2').remove().after('.section:nth-child(5)');

HTML:

<section class="photo small stamp stamp1">..</section>
<section class="photo small stamp stamp2">..</section>
<section class="photo small">..</section>
<section class="photo small">..</section>
<section class="photo small">..</section>
<section class="photo small">..</section>
<section class="photo small">..</section>
<section class="photo small">..</section>
<section class="photo small">..</section>
<section class="photo small">..</section>
<section class="photo small">..</section>
<section class="photo small">..</section>
<section class="photo small">..</section>
<section class="photo small">..</section>

4 个答案:

答案 0 :(得分:5)

删除.remove()位,只需使用after()它将移动节点:

DEMO

$('section:nth-child(5)').after($('.stamp2'))

答案 1 :(得分:1)

尝试.insertAfter()

$('.stamp2').detach().insertAfter('.section:nth-child(5)')

答案 2 :(得分:1)

您无需将其删除。只需使用after函数就可以了。

$('.stamp2').after($('.section:nth-child(5)'));

答案 3 :(得分:0)

使用Detach()代替Remove(),然后使用InsertAfter()