简体   繁体   English

jQuery将div附加到特定的

[英]jQuery append div to a specific

How do I append a element a specific index of the children elements using jQuery append eg if I have: 如何使用jQuery append将元素追加到子元素的特定索引,例如,如果我有:

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

and I want to append another item between the second and third item? 我想在第二个和第三个项目之间追加另一个项目?

There are a few options: 有几个选择:

$("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'));

The same options, but inserting into the same position using "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'));

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

$('.container').children()[1].append('whatever');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM