我有一个像这样的表格行
这一行是使用 jquery 中的 for 循环生成的,可以超过 1 行。我想删除除每行第一个元素之外的所有元素,我已经尝试过这样的
for (var i = 0; i < o.result[0].length; i++) {
//$("#fullSelection"+i).empty(); //its working
//$("#fullSelection"+i).not(':first').remove();//not working
$("#fullSelection" + i).slice(1).remove(); //not working
}
<tr id='fullSelection0' style='display: table-row;'>
<th class='predict'>
</th>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
知道如何实现它吗?
最佳答案
HTML 表格有两种单元格:
标题单元格 - 包含标题信息(使用 <th>
元素创建)
标准单元格 - 包含数据(使用 <td>
元素创建)
在 HTML 表格设计中,不应将 th 和 td 组合在同一个 tr 中。
我认为你不需要为此循环。您可以简单地定位并删除每个 tr
中除第一个之外的所有子项。使用 jQuery 选择器,例如:
$('tr td,th').not(':first-child').remove();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr id='fullSelection0' style='display: table-row;'>
<th class='predict'>Col-Test</th>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr id='fullSelection1' style='display: table-row;'>
<th class='predict'>Col-Test2</th>
<td>11</td>
<td>22</td>
<td>33</td>
</tr>
</table>
关于javascript - 如何从表行中删除除第一个元素之外的所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53202495/