10

jQuery append を使用して子要素の特定のインデックスに要素を追加するにはどうすればよいですか?たとえば、次の場合:

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

2番目と3番目のアイテムの間に別のアイテムを追加したいですか?

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'));

同じオプションですが、「before」を使用して同じ位置に挿入します。

$("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 に答える