简体   繁体   English

如何在html表中添加数字

[英]how can I add numbers in an html table

I'm trying to add a column of numbers in an HTML table. 我正在尝试在HTML表中添加一列数字。 I don't receive an errors in the console, I simply do not get a sum. 我没有在控制台中收到错误,我只是没有得到一笔款项。 here is a snippet of the HTML, and the JS in question. 这是HTML和相关JS的代码段。 As you can see each cell in the column has its own class, mile1, mile2, etc... There are 48 of these in the full HTML. 如您所见,列中的每个单元格都有其自己的类mile1,mile2等。完整的HTML中有48个。 The script runs without any problems and does what it should do, it just doesn't give a sum of the column "Miles". 该脚本运行时没有任何问题,并且执行了应做的事情,只是没有提供“ Miles”列的总和。

<body>
    <div id="map" style="height:400px"></div>
    <div id="status"></div>
    <div id="results"></div>
    <div id="table"></div>
    <div id="td"</div>

    <div style=" text-align: center; text-indent: 0px; padding: 0px 0px 0px 0px; margin: 0px 0px 0px 0px;">

        <table width="20%" border="1" cellpadding="0" cellspacing="0" "border-color: #000000; border-style: solid; background-color: #ffffff;">
            <tr>
                <th>State</th>
                   <th>Miles</th>       

                <tr valign="top">
                <td style="border-color : #000000 #000000 #000000 #000000; border-style: solid;"class="state1"><br />
                </td>
                <td style="border-color : #000000 #000000 #000000 #000000; border-style: solid;"class="mile1"><br />
                </td>
                <tr valign="top">
                    <td style="border-color : #000000 #000000 #000000 #000000; border-style: solid;" class="state2"><br />
                    </td>
                    <td style="border-color : #000000 #000000 #000000 #000000; border-style: solid;" class="mile2"><br />
                    </td>
                    <tr valign="top">





                       var x =(stateMiles[state]);

                     (stateMiles[state])= parseFloat(x);    

                     var m = (stateMiles[state]);
                     var n = Number(m);
                     var r = Math.round(m);
                     (stateMiles[state]) = Number(m);
                     (stateMiles[state]) = Math.round(m);   



                        $("#results").append;
                        $(".state" + i).append(state);
                        $(".mile" + i).append(stateMiles[state]);

//here is what i have been trying // //这是我一直在尝试的//

        var numbers = [(stateMiles[state])];                                }

        function getSum(total, num) {
    return total + Math.round(num);
    document.getElementById("table").td(mile1,mile2) = numbers.reduce(getSum,0);

Sadly neither your code or your JSFiddle is a small / minimal working example. 遗憾的是,您的代码或JSFiddle都不是一个很小的工作实例。

Generally speaking you should be able to do what you want as long as you watch for the two following pitfalls: 一般来说,只要注意以下两个陷阱,您就应该能够做自己想做的事情:

  1. A table has a tbody child-element (so a row is not a direct child of a table) 一个表具有一个tbody子元素(因此,行不是该表的直接子元素)
  2. You might need to parse the content of a Cell into a number 您可能需要将单元格的内容解析为数字

Considering this, the following snippet gets a value from a table. 考虑到这一点,以下代码段从表中获取一个值。 Making it a sum of those should be done by simply running over the table domNode "array" 只需通过在表domNode“ array”上运行即可完成这些工作的总和。

<table id="table">
    <tr><td>1</td><td>2</td></tr>
    <tr><td>3</td><td>4</td></tr>
</table>

To get the first value you need: 要获得第一个价值,您需要:

parseInt(document.getElementById("table").children[0].children[0].children[0].textContent)

The children chaining is table->tbody->tr->td 子链是table-> tbody-> tr-> td

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM