【问题标题】:Read td values by tr id from the same table从同一个表中通过 tr id 读取 td 值
【发布时间】:2014-11-05 13:04:47
【问题描述】:

我正在尝试根据 tr id 读取 table 的值,但无法理解如何执行此操作。

    // 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); 

}

【问题讨论】:

    标签: javascript jquery row


    【解决方案1】:

    既然你用 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());
    });
    

    【讨论】:

    • 是的,这很适合我。非常感谢你。我会尽快接受答案。
    【解决方案2】:

    以防万一您只需要 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);
    }
    

    Demo

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-05
      • 1970-01-01
      • 1970-01-01
      • 2014-09-23
      • 1970-01-01
      • 1970-01-01
      • 2014-06-29
      相关资源
      最近更新 更多