簡體   English   中英

從同一張表中按tr id讀取td值

[英]Read td values by tr id from the same table

我正在嘗試根據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);
}

演示版

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM