jQuery将div附加到特定的

ip.*_*ip. 9 javascript jquery append appendto

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

<div class=".container">
   <div class="item"><div>
   <div class="item"><div>
   <div class="item"><div>
   <div class="item"><div>
</div>
Run Code Online (Sandbox Code Playgroud)

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

Joh*_*her 19

有几个选择:

$("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'));
Run Code Online (Sandbox Code Playgroud)

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

$("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'));
Run Code Online (Sandbox Code Playgroud)