我一直在尝试为我的表行建立索引,但没有成功。我能做的最好的事情就是提出代码,每次我添加新的表行时,它都可以重新索引表中的每一行,但它不起作用。我认为语法有问题,但我不确定是什么。 JS部分:
$("#pridet").click(function(){
$("table tbody tr").first().clone().prependTo("table tbody");
var x = document.getElementsByTagName("tr");
document.getElementsByName("tabelis").innerHTML = x.rowIndex;
});
我意识到第二部分存在 fatal error (例如指示在哪里编写索引?)
html 文件中的代码:
<button id = "pridet">pridet</button>
<table id="myTable" class="table table-inverse">
<thead id = "headings" class = "thead-default">
<tr>
<th>Tabelio Nr.</th>
<th>Vardas</th>
<th>Pavardė</th>
<th>Pareigos</th>
<th>Bazinė alga, €</th>
<th>Valandinis atlyginimas, €</th>
<th>Veiksmai</th>
</tr>
</thead>
<tfoot class = "thead-default">
<tr>
<td>Vidurkis</td>
<td></td>
<td></td>
<td></td>
<td>10000</td>
<td>17.3</td>
<td></td>
</tr>
</tfoot>
<tbody>
<tr>
<td class = "tabelis">1</td>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
<td>bla</td>
<td><input type="button" value="Delete" onclick="deleteRow(this)"></td>
<td>
<p class = "btn" style = "width:10%; padding-bottom:0; margin-bottom:0; border-bottom:0"><span class = "fa fa-edit"></span></p>
<p class = "btn" style = "width:10%; padding-bottom:0; margin-bottom:0; border-bottom:0" onclick="deleteRow(this)"><span class = "fa fa-trash"></span></p>
</td>
</tr>
<tr>
<td class = "tabelis">2</td>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
<td>bla</td>
<td>blum</td>
<td>
<!-- <p class = "btn" style = "width:10%; padding-bottom:0; margin-bottom:0; border-bottom:0"><span class = "fa fa-edit"></span></p> -->
<!-- <p class = "btn" style = "width:10%; padding-bottom:0; margin-bottom:0; border-bottom:0"><span class = "fa fa-trash"></span></p> -->
</td>
</tr>
<tr>
<td class = "tabelis">3</td>
<td>Larry</td>
<td>the Bird</td>
<td>@twitter</td>
<td>bla</td>
<td>blum</td>
<td>
<!-- <p class = "btn" style = "width:10%; padding-bottom:0; margin-bottom:0; border-bottom:0"><span class = "fa fa-edit"></span></p> -->
<!-- <p class = "btn" style = "width:10%; padding-bottom:0; margin-bottom:0; border-bottom:0"><span class = "fa fa-trash"></span></p> -->
</td>
</tr>
</tbody>
</table>
<script src="script.js" charset="utf-8"></script>
<script>
function deleteRow(r) {
var i = r.parentNode.parentNode.rowIndex;
document.getElementById("myTable").deleteRow(i);
};
</script>
我可能可以用这样的方法来计算总行数
$("#counter").text( $("table tbody tr").length );
并使用该值以某种方式将数字插入表格单元格
编辑:之前没有提到这一点,但是表格行必须添加到表格顶部(标题下方)
最佳答案
$("#pridet").click(function(){
var new_row = $("table tbody tr").first().clone();
$('table tbody').append(new_row);
$('table tr:last').find('td:first').html($('table tbody tr').length);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button id = "pridet">pridet</button>
<table id="myTable" class="table table-inverse" border='1'>
<thead id = "headings" class = "thead-default">
<tr>
<th>Tabelio Nr.</th>
<th>Vardas</th>
<th>Pavardė</th>
<th>Pareigos</th>
<th>Bazinė alga, €</th>
<th>Valandinis atlyginimas, €</th>
<th>Veiksmai</th>
</tr>
</thead>
<tfoot class = "thead-default">
<tr>
<td>Vidurkis</td>
<td></td>
<td></td>
<td></td>
<td>10000</td>
<td>17.3</td>
<td></td>
</tr>
</tfoot>
<tbody>
<tr>
<td class = "tabelis">1</td>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
<td>bla</td>
<td><input type="button" value="Delete" onclick="deleteRow(this)"></td>
<td>
<p class = "btn" style = "width:10%; padding-bottom:0; margin-bottom:0; border-bottom:0"><span class = "fa fa-edit"></span></p>
<p class = "btn" style = "width:10%; padding-bottom:0; margin-bottom:0; border-bottom:0" onclick="deleteRow(this)"><span class = "fa fa-trash"></span></p>
</td>
</tr>
<tr>
<td class = "tabelis">2</td>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
<td>bla</td>
<td>blum</td>
<td>
<!-- <p class = "btn" style = "width:10%; padding-bottom:0; margin-bottom:0; border-bottom:0"><span class = "fa fa-edit"></span></p> -->
<!-- <p class = "btn" style = "width:10%; padding-bottom:0; margin-bottom:0; border-bottom:0"><span class = "fa fa-trash"></span></p> -->
</td>
</tr>
<tr>
<td class = "tabelis">3</td>
<td>Larry</td>
<td>the Bird</td>
<td>@twitter</td>
<td>bla</td>
<td>blum</td>
<td>
<!-- <p class = "btn" style = "width:10%; padding-bottom:0; margin-bottom:0; border-bottom:0"><span class = "fa fa-edit"></span></p> -->
<!-- <p class = "btn" style = "width:10%; padding-bottom:0; margin-bottom:0; border-bottom:0"><span class = "fa fa-trash"></span></p> -->
</td>
</tr>
</tbody>
</table>
关于javascript - 索引表行 js/jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44391768/