问题上插入一排表

我想插入一行到多个表的问题上插入一排表

$('table > tbody > tr:first').before('<tr><td>Stuff</td></tr>'); 

是增加新tr只有第一个表和

$("tbody").each(function() { 

$('table > tbody > tr:first').before('<tr><td>Stuff</td></tr>');

});

的开头开始再次将所有新的tr s添加到第一个表。你能让我知道如何解决这个问题吗?

$("tbody").each(function() {  

$('table > tbody > tr:first').before('<tr><td>Stuff</td></tr>');

});

$('table > tbody > tr:first').before('<tr><td>Stuff</td></tr>');

table,  

th,

td {

border: 1px solid #eee;

}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  

<table>

<tbody>

<tr>

<td>January</td>

<td>$100</td>

</tr>

<tr>

<td>February</td>

<td>$80</td>

</tr>

</tbody>

</table>

<table>

<tbody>

<tr>

<td>January</td>

<td>$100</td>

</tr>

<tr>

<td>February</td>

<td>$80</td>

</tr>

</tbody>

</table>

<table>

<tbody>

<tr>

<td>January</td>

<td>$100</td>

</tr>

<tr>

<td>February</td>

<td>$80</td>

</tr>

</tbody>

</table>

<table>

<tbody>

<tr>

<td>January</td>

<td>$100</td>

</tr>

<tr>

<td>February</td>

<td>$80</td>

</tr>

</tbody>

</table>

回答:

使用$(this)来引用每个表。

$("tbody ").each(function() {  

$(this).before('<tr><td>Stuff</td></tr>');

});

table,  

th,

td {

border: 1px solid #eee;

}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  

<table>

<tbody>

<tr>

<td>January</td>

<td>$100</td>

</tr>

<tr>

<td>February</td>

<td>$80</td>

</tr>

</tbody>

</table>

<table>

<tbody>

<tr>

<td>January</td>

<td>$100</td>

</tr>

<tr>

<td>February</td>

<td>$80</td>

</tr>

</tbody>

</table>

<table>

<tbody>

<tr>

<td>January</td>

<td>$100</td>

</tr>

<tr>

<td>February</td>

<td>$80</td>

</tr>

</tbody>

</table>

<table>

<tbody>

<tr>

<td>January</td>

<td>$100</td>

</tr>

<tr>

<td>February</td>

<td>$80</td>

</tr>

</tbody>

</table>

PS - 如果你想这里面tbody您可以使用$(this).find("tr:first").before('<tr><td>Stuff</td></tr>');

回答:

您使用的是相同的选择,而不是tbody

$("tbody").each(function() { 

$(this).before('<tr><td>Stuff</td></tr>');

});

回答:

$("tbody").each(function() {  

// I don't want to insert before the tbody,

// I want to insert before the first row.

$(this).find("tr:first").before('<tr><td>Stuff</td></tr>');

});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  

<table>

<tbody>

<tr>

<td>January</td>

<td>$100</td>

</tr>

<tr>

<td>February</td>

<td>$80</td>

</tr>

</tbody>

</table>

<table>

<tbody>

<tr>

<td>January</td>

<td>$100</td>

</tr>

<tr>

<td>February</td>

<td>$80</td>

</tr>

</tbody>

</table>

<table>

<tbody>

<tr>

<td>January</td>

<td>$100</td>

</tr>

<tr>

<td>February</td>

<td>$80</td>

</tr>

</tbody>

</table>

<table>

<tbody>

<tr>

<td>January</td>

<td>$100</td>

</tr>

<tr>

<td>February</td>

<td>$80</td>

</tr>

</tbody>

</table>

以上是 问题上插入一排表 的全部内容, 来源链接: utcz.com/qa/266859.html

回到顶部