我如何在 jQuery 3.4.1 或 vanilla JS 中重写这样的东西:
$("#ResultsTable tr:not(:first)").remove();
with :first 被弃用了吗? ( https://api.jquery.com/first-selector/ )
我需要删除除第一行之外的所有行。
最佳答案
您可以使用.slice(startIndex)
方法
$("#ResultsTable tr").slice(1).remove();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="ResultsTable">
<tr><td>1</td></tr>
<tr><td>2</td></tr>
<tr><td>3</td></tr>
<tr><td>4</td></tr>
</table>
如果第一行是标题,则使用表结构 thead
和 tbody并使用.empty()
删除正文中的行。
$("#ResultsTable tbody").empty();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="ResultsTable">
<thead>
<tr><th>1</th></tr>
</thead>
<tbody>
<tr><td>2</td></tr>
<tr><td>3</td></tr>
<tr><td>4</td></tr>
</tbody>
</table>
关于jQuery :first deprecated - rewrite?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59631736/