[英]How to insert after 2nd element?
對於此標記,如何在mform
類的第二個表單之后插入一些內容:
<table id="sortme">
<tr>
<td>1</td>
<td><form class="mform"></form></td>
</tr>
<tr>
<td>2</td>
<td><form class="mform"></form><!---Insert Me here !---></td>
</tr>
<tr>
<td>3</td>
<td><form class="mform"></form></td>
</tr>
</table>
要在第一個表單后插入,我可以使用:
$(function () {
$('<div>block</div>').insertAfter("#sortme form.mform:first");
});
所以我已經嘗試了第二個代碼,但沒有用:
$(function () {
$('<div>block</div>').insertAfter("#sortme > form.mform:nth-child(2)");
});
嘗試.insertAfter("#sortme form.mform:eq(1)");
您也可以使用counter
,條件語句並append
如下:
<script>
$(document).ready(function(){
counter = 0;
$(".mform").each(function(){
if (counter === 1){
$(this).append("<div>Block</div>");
}
counter++;
});
});
</script>
一個演示是這里
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.