Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 views
in Technique[技术] by (71.8m points)

javascript - Auto-number table rows?

I have the following HTML table:

<table border="1">
  <tr>
    <td>blue</td>
  </tr>
  <tr>
    <td>red</td>
  </tr>
  <tr>
    <td>black</td>
  </tr>
</table>

I would like each row in this table have a number automatically assigned to each item.

How could he do?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The following CSS enumerates table rows (demo):

table {
  counter-reset: rowNumber;
}

table tr::before {
  display: table-cell;
  counter-increment: rowNumber;
  content: counter(rowNumber) ".";
  padding-right: 0.3em;
  text-align: right;
}
<table cellpadding="0">
  <tr><td>blue</td></tr>
  <tr><td>red</td></tr>
  <tr><td>yellow</td></tr>
  <tr><td>green</td></tr>
  <tr><td>purple</td></tr>
  <tr><td>orange</td></tr>
  <tr><td>maroon</td></tr>
  <tr><td>mauve</td></tr>
  <tr><td>lavender</td></tr>
  <tr><td>pink</td></tr>
  <tr><td>brown</td></tr>
</table>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...