gpt4 book ai didi

javascript - 如何添加自动编号表行?

转载 作者:太空宇宙 更新时间:2023-11-03 19:42:18 25 4
gpt4 key购买 nike

就我而言,我的 HTML 和 javascript:

$('.addRow').on('click', function() {
addRow();
});

function addRow() {
var tr = '<tr>' +
'<td></td>' +
'</tr>';
$('tbody').append(tr);
};
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<table id="example1" class="table table-bordered">
<thead>
<th>No.</th>
<th style="text-align:center"><a href='#' class="addRow"><i class="glyphicon glyphicon-plus"></i></a></th>
</thead>
</table>

我想做成这样

1.

2.

3.

......................

最佳答案

你可以为此使用 css 计数器

检查下面的代码片段

$('.addRow').on('click', function() {
addRow();
});

function addRow() {
var tr = '<tr>' +
'<td>hello</td>' +
'</tr>';
$('table tbody').append(tr);
};
tbody {
counter-reset: row;
/* Set the row counter to 0 */
}

tbody tr::before {
counter-increment: row;
/* Increment the row counter*/
content: counter(row) ": ";
/* Display the row */
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="example1" class="table table-bordered">
<thead>
<th>No.</th>
<th style="text-align:center"><a href='#' class="addRow">AddRow</a></th>
</thead>
<tbody></tbody>
</table>

关于javascript - 如何添加自动编号表行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42796083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com