如何在特定列的 HTML 表格中显示行号?

use*_*792 1 html javascript row row-number

我有一个包含 5 列的 HTML 表格。第一列是“行号”,我想在其中显示它是哪一行——从 1 开始。

这是一张图片

我试过使用这个 CSS:

body {
    /* Set the Serial counter to 0 */
    counter-reset: Serial; 
}

table {
    border-collapse: separate;
}

tr td:first-child:before {
    /* Increment the Serial counter */
    counter-increment: Serial;

    /* Display the counter */
    content: "Serial is: " counter(Serial); 
}
Run Code Online (Sandbox Code Playgroud)

Kir*_*rev 6

您可以再次使用通过 css 的下一个选项:注意:class="css-serial"

<table class="css-serial">
  <thead>
    <tr>
      <th>#</th>
      <th>1st Column</th>
      <th>2nd Column</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td></td>           <!--intentionally left blank-->
      <td>Column 1</td>
      <td>Column 2</td>
    </tr>
    <tr>
      <td></td>           <!--intentionally left blank-->
      <td>Column 1</td>
      <td>Column 2</td>
    </tr>
    <tr>
      <td></td>           <!--intentionally left blank-->
      <td>Column 1</td>
      <td>Column 2</td>
    </tr>
  </tbody>
</table>
Run Code Online (Sandbox Code Playgroud)

并添加下一个样式:

 <style>
/*adding row numbers through css*/
.css-serial {
    counter-reset: serial-number; /* Set the serial number counter to 0 */
}

    .css-serial td:first-child:before {
        counter-increment: serial-number; /* Increment the serial number counter */
        content: counter(serial-number); /* Display the counter */
    }

</style>
Run Code Online (Sandbox Code Playgroud)