我需要在三列,表格式枚举环境中的HTML页面中显示一些数据,如下所示:
1. elephant animal a large animal that
eats leaves
2. fish animal an animal that
swims in the ocean
3. cactus plant a plant that lives
in dry places
是否有用于呈现枚举表格环境的干净HTML或CSS解决方案?
答案 0 :(得分:3)
您可以使用CSS计数器功能自动生成数字,如下例所示:
table{
counter-reset: rows; /* initalize the counter variable */
}
tr{
counter-increment: rows; /* increment the counter every time a tr is encountered */
}
td{ /* just for demo */
vertical-align: top;
width: 100px;
}
td:first-child:before{ /* add the counter value before the first td in every row */
content: counter(rows) ". ";
}
注意:
答案 1 :(得分:1)
您可以使用CSS执行此操作:
table {
counter-reset: rowNumber;
}
table tr {
counter-increment: rowNumber;
}
table tr td:first-child::before {
content: counter(rowNumber);
min-width: 1em;
margin-right: 0.5em;
}
但我建议JS:
var table = document.getElementsByTagName('table')[0],
rows = table.getElementsByTagName('tr'),
text = 'textContent' in document ? 'textContent' : 'innerText';
console.log(text);
for (var i = 0, len = rows.length; i < len; i++){
rows[i].children[0][text] = i + ': ' + rows[i].children[0][text];
}