从同一个表中按tr id读取td值

Read td values by tr id from the same table

本文关键字:id 读取 td tr 同一个      更新时间:2023-09-26

我正试图根据tr-id读取表的值,但无法理解如何做到这一点。

    // Id of the tr in question for example.row_17 
    var d =c["id"]; 
    console.log(d); 
    // Here I can read all the td values, but I only want
    // the ones inside the tr id = "row_17" 
    var cols =document.getElementById('report_table').getElementsByTagName('td'), 
     colslen = cols.length, i = -1; > while(++i < colslen)
    {  console.log(cols[i].innerHTML); 
}

由于您使用jQuery对其进行了标记,因此可以通过以下操作来完成:

var id = c["id"];
// Select only the row with id specified in 'id' and loop through all 'td's' of that row.
$("#" + id).find("td").each(function()
{
    // Log the html content of each 'td'.
    console.log($(this).html());
});

如果您只想要一个JavaScript解决方案(没有jQuery):

var id = c["id"];
var tds = document.querySelectorAll('#' + id + ' td');
for(var i = 0; i < tds.length; i++) {
    console.log(tds[i].innerHTML);
}

演示