10

如何使用 jQuery append 将元素附加到子元素的特定索引,例如,如果我有:

<div class=".container">
   <div class="item"><div>
   <div class="item"><div>
   <div class="item"><div>
   <div class="item"><div>
</div>

我想在第二个和第三个项目之间附加另一个项目?

4

3 回答 3

20

有几个选项:

$("div.container div.item").eq(2).after($('your html'));

$("div.container div.item:nth-child(3)").after($('your html'));

$($("div.container div.item")[2]).after($('your html'));

相同的选项,但使用“之前”插入相同的位置:

$("div.container div.item").eq(3).before($('your html'));

$("div.container div.item:nth-child(4)").before($('your html'));

$($("div.container div.item")[3]).before($('your html'));
于 2010-05-05T20:19:19.450 回答
4

("div.container div.item:eq(1)").after("<element to insert>")

于 2010-05-05T20:17:31.783 回答
-1
$('.container').children()[1].append('whatever');
于 2010-05-05T20:18:43.897 回答