计算每个打印表的行数

Count number of rows for each printed table

本文关键字:打印 计算      更新时间:2023-09-26

我需要你的助手在我的html中获得每个打印表的行总数,以显示在它的顶部。

我在html中有多个表,它们的id是唯一的"detailsTable"。每个表有不同的行数。我搜索一个脚本来打印行总数,我找到了下面的脚本:

function count()
{
var rows = document.getElementById("detailsTable").getElementsByTagName("tr").length;
alert(rows); 
}       
在body标签中,我放置了:
<body onload="count()">

当我运行页面时,它显示了第一个表的警告,它有22条记录,脚本忽略了下面的表。

所以,你能帮我修改上面的代码,并显示其他表的警报。

每个html元素的id必须是唯一的。

你可以用class代替,然后:

function count()
{
var tables = document.getElementsByClassName("detailsTable");
var rows;
for (var i = 0; i < tables.length; i++) {
  rows = tables[i].rows.length;
  alert(rows); 
}
}  

如果您想对id =" detailsTable"的表行进行计数

var rowCount = $('#detailsTable tr').length;

如果你想为每个表找到它,它看起来像这样:

$('.details').each(function(index) {
   var rowCount = this.rows.length;
})