Javascript 索引表行js/jquery
我一直在尝试索引我的表行,但没有成功。我能做的最好的事情就是编写代码,每次我添加新的表行时,可能会重新索引表中的每一行,但它不起作用。我认为语法有问题,但我不确定是什么。JS部分:Javascript 索引表行js/jquery,javascript,jquery,html,Javascript,Jquery,Html,我一直在尝试索引我的表行,但没有成功。我能做的最好的事情就是编写代码,每次我添加新的表行时,可能会重新索引表中的每一行,但它不起作用。我认为语法有问题,但我不确定是什么。JS部分: $("#pridet").click(function(){ $("table tbody tr").first().clone().prependTo("table tbody"); var x = document.getElementsByTagName("tr"); document.getEle
$("#pridet").click(function(){
$("table tbody tr").first().clone().prependTo("table tbody");
var x = document.getElementsByTagName("tr");
document.getElementsByName("tabelis").innerHTML = x.rowIndex;
});
我意识到第二部分出了致命的问题(比如指导在哪里写什么索引?)
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>
并使用该值以某种方式将数字插入表格单元格
编辑:之前没有提到这一点,但必须在表的顶部(在A下方)添加表行。
$(“#pridet”)。单击(函数(){
var new_row=$(“表tbody tr”).first().clone();
$('table tbody')。追加(新行);
$('table tr:last').find('td:first').html($('table tbody tr').length);
});代码>
普里代特
塔贝利奥。
瓦尔达斯
帕瓦德
帕雷戈斯
巴赞阿尔加
亚特兰蒂斯岛
维克斯迈
维杜基斯
10000
17.3
1.
做记号
奥托
@mdo
布拉
2.
雅各布
桑顿
@肥
布拉
布鲁姆
3.
拉里
鸟
@推特
布拉
布鲁姆
$(“#pridet”)。单击(函数(){
var new_row=$(“表tbody tr”).first().clone();
$('table tbody')。追加(新行);
$('table tr:last').find('td:first').html($('table tbody tr').length);
});代码>
普里代特
塔贝利奥。
瓦尔达斯
帕瓦德
帕雷戈斯
巴赞阿尔加
亚特兰蒂斯岛
维克斯迈
维杜基斯
10000
17.3
1.
做记号
奥托
@mdo
布拉
2.
雅各布
桑顿
@肥
布拉
布鲁姆
3.
拉里
鸟
@推特
布拉
布鲁姆
因为在正文行的第一列中有行索引,所以您可以使用以下选项选择每一行:
$('#myTable tbody tr td:nth-child(1)')
您可以使用循环迭代每个索引以更新它
函数updateRowIndex(){
$(“#myTable tbody tr td:n个子项(1)”。每个(函数(idx,ele){
ele.textContent=idx+1;
});
}
函数deleteRow(r){
var i=r.parentNode.parentNode.rowIndex;
document.getElementById(“myTable”).deleteRow(i);
updaterovindex();
};
$(“#pridet”)。在('click',函数(e){
$(“表tbody tr”).first().clone().prependTo(“表tbody”);
var x=document.getElementsByTagName(“tr”);
document.getElementsByName(“tabelis”).innerHTML=x.rowIndex;
updaterovindex();
});代码>
普里代特
塔贝利奥。
瓦尔达斯
帕瓦德
帕雷戈斯
巴赞阿尔加
亚特兰蒂斯岛
维克斯迈
维杜基斯
10000
17.3
1.
做记号
奥托
@mdo
布拉
2.
雅各布
桑顿
@肥
布拉
布鲁姆
3.
拉里
鸟
@推特
布拉
布鲁姆
因为在正文行的第一列中有行索引,所以您可以使用以下选项选择每一行:
$('#myTable tbody tr td:nth-child(1)')
您可以使用循环迭代每个索引以更新它
函数updateRowIndex(){
$(“#myTable tbody tr td:n个子项(1)”。每个(函数(idx,ele){
ele.textContent=idx+1;
});
}
函数deleteRow(r){
var i=r.parentNode.parentNode.rowIndex;
document.getElementById(“myTable”).deleteRow(i);
updaterovindex();
};
$(“#pridet”)。在('click',函数(e){
$(“表tbody tr”).first().clone().prependTo(“表tbody”);
var x=document.getElementsByTagName(“tr”);
document.getElementsByName(“tabelis”).innerHTML=x.rowIndex;
updaterovindex();
});代码>
普里代特
塔贝利奥。
瓦尔达斯
帕瓦德
帕雷戈斯
巴赞阿尔加
亚特兰蒂斯岛
维克斯迈
维杜基斯
10000
17.3
1.
做记号
奥托
@mdo
布拉
2.
雅各布
桑顿
@肥
布拉
布鲁姆