admin管理员组文章数量:1136497
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?
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?
Share Improve this question edited Sep 16, 2016 at 21:36 BalusC 1.1m376 gold badges3.6k silver badges3.6k bronze badges asked Jun 9, 2013 at 17:45 MarcioMarcio 1,7333 gold badges25 silver badges39 bronze badges 4- Why? that's a static table of three row, just type 1, 2 ,3 – Axel Amthor Commented Jun 9, 2013 at 17:47
- You can't unless you use some scripting. – Lion Commented Jun 9, 2013 at 17:47
- Can you specify what do you want exactly, and for wich reason you want to achieve this. That will help us to help you more effciency. – Vinc 웃 Commented Jun 9, 2013 at 17:53
- 1 Ok, but what I wanted was a number, 1,2,3,4 ... automatically each row. :( – Marcio Commented Jun 9, 2013 at 17:54
4 Answers
Reset to default 126The 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>
If the CSS cannot be used, try the following JavaScript code (demo):
var table = document.getElementsByTagName('table')[0],
rows = table.getElementsByTagName('tr'),
text = 'textContent' in document ? 'textContent' : 'innerText';
for (var i = 0, len = rows.length; i < len; i++) {
rows[i].children[0][text] = i + ': ' + rows[i].children[0][text];
}
<table border="1">
<tr>
<td>blue</td>
</tr>
<tr>
<td>red</td>
</tr>
<tr>
<td>black</td>
</tr>
</table>
And if you would use headers as well the following is the thing you need: http://jsfiddle.net/davidThomas/7RyGX/
table {
counter-reset: rowNumber;
}
table tr:not(:first-child) {
counter-increment: rowNumber;
}
table tr td:first-child::before {
content: counter(rowNumber);
min-width: 1em;
margin-right: 0.5em;
}
note the: ":not(:first-child)" in there.
Here is a modification of David Thomas' CSS solution that works with or without a header row in the table. It increments the counter on the first td
cell of each row (thereby skipping the row with only th
cells):
table
{
counter-reset: rowNumber;
}
table tr > td:first-child
{
counter-increment: rowNumber;
}
table tr td:first-child::before
{
content: counter(rowNumber);
min-width: 1em;
margin-right: 0.5em;
}
You can see the behavior in this jsfiddle.
Here's a javascript solution that will add a cell at the beginning of each row , this cell will be used for numbering, if there is a th
cell this gets a colspan=2
attribute.
var addNumeration = function(cl){
var table = document.querySelector('table.' + cl)
var trs = table.querySelectorAll('tr')
var counter = 1
Array.prototype.forEach.call(trs, function(x,i){
var firstChild = x.children[0]
if (firstChild.tagName === 'TD') {
var cell = document.createElement('td')
cell.textContent = counter ++
x.insertBefore(cell,firstChild)
} else {
firstChild.setAttribute('colspan',2)
}
})
}
addNumeration("test")
<table class="test" border="1">
<tr>
<th>hi!</th>
</tr>
<tr>
<td>blue</td>
</tr>
<tr>
<td>red</td>
</tr>
<tr>
<td>black</td>
</tr>
</table>
本文标签: javascriptAutonumber table rowsStack Overflow
版权声明:本文标题:javascript - Auto-number table rows? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736966516a1957919.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论