简体   繁体   English

如何遍历JavaScript中的表行和单元格并添加值?

[英]How do I iterate through table rows and cells in JavaScript and add the values?

This is my HTML table, on the table I have 3 rows and 3 columns, what I am trying to do is add the first two columns together and put the result in the third column. 这是我的HTML表格,在该表格上我有3行3列,我想要做的是将前两列加在一起并将结果放在第三列中。 Any help would be greatly appreciated. 任何帮助将不胜感激。

CSS: CSS:

<style type="text/css">
  .tg  { border-collapse:collapse; border-spacing:0; border-color:#bbb; }
  .tg td { font-family:Arial, sans-serif; font-size:14px; padding:10px 5px;border-
style:solid; border-width:0px; overflow:hidden; word-break:normal; border-
color:#bbb; color:#594F4F; background-color:#E0FFEB; }
  .tg th { font-family:Arial, sans-serif; font-size:14px; font-
weight:normal; padding:10px 5px; border-style:solid; border-
width:0px; overflow:hidden; word-break:normal; border-
color:#bbb; color:#493F3F; background-color:#9DE0AD; }
  .tg .tg-jf1j { font-weight:bold; font-size:20px; color:#ffccc9; text-
align:center; vertical-align:top; }
  .tg .tg-yw4l { vertical-align:top; }
</style>

HTML: HTML:

<table class="tg">
    <tr>
        <th class="tg-yw4l">1</th>
        <th class="tg-yw4l">1</th>
        <th id="a"></th>
    </tr>
    <tr>
        <td class="tg-yw4l">1</td>
        <td class="tg-yw4l">2</td>
        <td id="a"></td>
    </tr>
    <tr>
        <td class="tg-yw4l">2</td>
        <td class="tg-yw4l">3</td>
        <td id="a"></td>
    </tr>
</table>

Below here is where I try looping through my table and adding the first two columns together. 下面是我尝试遍历表并将前两列加在一起的地方。

<script type="text/javascript">

    var table = document.getElementsByClassName("tg");

    for (var r = 0; r < 3; r++) {
        var sum1 = 0;

        for (var c = 0; c < 3; r++) {
            if (table[r][c].className == 'tg-yw41') {
                sum1 += isNaN(table[r][c].innerHTML) ? 0 : parseInt(table[r][c].innerHTML);
            }
        }

        document.getElementById('a').innerHTML = sum1;
    }
 </script>

Below code will add first and second td/th and show sum in third td/th . 下面的代码将添加第一和第二td/th并以第三td/th显示总和。

Note : You can further add error handling of Nan and other things. 注意:您可以进一步添加Nan和其他内容的错误处理。

Jquery Code : jQuery代码:

$('table.tg tr').each(function() {
    var third_var = parseInt($(this).find('th:first-child, td:first-child').html()) + parseInt($(this).find('th:nth-child(2), td:nth-child(2)').html());
    $(this).find('th:nth-child(3), td:nth-child(3)').html(third_var);
});

Javascript Code : Online Demo As requested by @Alex_89 Javascript代码: 在线演示(按@ Alex_89的要求)

var table = document.getElementsByClassName("tg");
var t  = table[0];
for (var r = 0; r < t.rows.length; r++) {
    t.rows[r].cells[2].innerHTML = parseInt(t.rows[r].cells[0].innerHTML) + parseInt(t.rows[r].cells[1].innerHTML);
}

Solution for your question 您问题的解决方案

 var table = document.getElementById("tg"); for (var i = 0, row; row = table.rows[i]; i++) { var sum1= 0; for (var j = 0, col; col = row.cells[j]; j++) { sum1+=(col.textContent!='')?parseInt(col.textContent):0; } var a='a'+(i+1).toString(); document.getElementById(a).innerHTML = sum1; sum1=0; } 
 .tg {border-collapse:collapse;border-spacing:0;border-color:#bbb;} .tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border- style:solid;border-width:0px;overflow:hidden;word-break:normal;border- color:#bbb;color:#594F4F;background-color:#E0FFEB;} .tg th{font-family:Arial, sans-serif;font-size:14px;font- weight:normal;padding:10px 5px;border-style:solid;border- width:0px;overflow:hidden;word-break:normal;border- color:#bbb;color:#493F3F;background-color:#9DE0AD;} .tg .tg-jf1j{font-weight:bold;font-size:20px;color:#ffccc9;text- align:center;vertical-align:top} .tg .tg-yw4l{vertical-align:top} 
 <table id="tg" class="tg"> <tr> <th class="tg-yw4l">1</th> <th class="tg-yw4l">1</th> <th id="a1"></th> </tr> <tr> <td class="tg-yw4l">1</td> <td class="tg-yw4l">2</td> <td id="a2"></td> </tr> <tr> <td class="tg-yw4l">2</td> <td class="tg-yw4l">3</td> <td id="a3"></td> </tr> </table> 

solution with plain js with th and td support 具有th和td支持的纯js解决方案

 window.onload = function(){ var table = document.getElementsByClassName("tg")[0]; var rows = table.getElementsByTagName('tr'); for(var i =0 ; i < rows.length;i++ ) { if(i == 0) { var cells = rows[i].getElementsByTagName('th'); cells[2].innerHTML = parseInt(cells[0].innerHTML) + parseInt(cells[1].innerHTML); continue; } var cells = rows[i].getElementsByTagName('td'); cells[2].innerHTML = parseInt(cells[0].innerHTML) + parseInt(cells[1].innerHTML); } } 
 .tg {border-collapse:collapse;border-spacing:0;border-color:#bbb;} .tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border- style:solid;border-width:0px;overflow:hidden;word-break:normal;border- color:#bbb;color:#594F4F;background-color:#E0FFEB;} .tg th{font-family:Arial, sans-serif;font-size:14px;font- weight:normal;padding:10px 5px;border-style:solid;border- width:0px;overflow:hidden;word-break:normal;border- color:#bbb;color:#493F3F;background-color:#9DE0AD;} .tg .tg-jf1j{font-weight:bold;font-size:20px;color:#ffccc9;text- align:center;vertical-align:top} .tg .tg-yw4l{vertical-align:top} 
 <style type="text/css"> </style> <table class="tg"> <tr> <th class="tg-yw4l">1</th> <th class="tg-yw4l">1</th> <th id="a"></th> </tr> <tr> <td class="tg-yw4l">1</td> <td class="tg-yw4l">2</td> <td id="a"></td> </tr> <tr> <td class="tg-yw4l">2</td> <td class="tg-yw4l">3</td> <td id="a"></td> </tr> </table> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何遍历 JavaScript 中的表格行和单元格? - How do I iterate through table rows and cells in JavaScript? 如何遍历具有特定img alt属性的表行并删除JavaScript中的单元格 - How do I iterate through table rows with specific img alt attribute and delete cells in javascript Javascript:如何遍历值数组并创建表行 - Javascript: How to iterate through an array of values and create table rows 使用JavaScript迭代表格以获取特定单元格的值 - Iterate through a table using JavaScript to get values of specific cells 如何使用javascript制作表格并向其中添加表格单元格? - How do i make an table with javascript and add table cells to it? Javascript / JQuery遍历表行和单元格,并将选中的复选框的属性输出到控制台 - Javascript/JQuery iterate through table rows and cells and output an attr of checked checkboxes to console 如何遍历表格行并使用 jQuery 获取单元格值 - How to iterate through a table rows and get the cell values using jQuery 如何通过Javascript显示输入列表行? - How do I display input column table rows through Javascript? 如何从 mysql 表中获取所有行,发送 json,遍历数组并显示? - How do I get all the rows from a mysql table, send json, iterate through array and display? 如何遍历表格行以获取所有元素值? - How do I iterate through a table row to get all elements values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM