ID | Field 1 | Field 2 | Field 3 | Field 4 |
1 | 11 | 12 | 13 | 14 |
2 | twenty two | 26 | 28 | 27 |
total |
<table id="mytable" border="1" width="37%">
<thead>
<tr>
<td width="63" >ID</td>
<td width="68" >字段1</td>
<td width="62" >字段2</td>
<td width="75" >字段3</td>
<td>字段4</td>
</tr>
</thead>
<tr>
<td>1</td>
<td width="63" >11</td>
<td width="68" >12</td>
<td width="62" >13</td>
<td width="75" >14</td>
</tr>
<tr>
<td>2</td>
<td width="63" >22</td>
<td width="68" >26</td>
<td width="62" >28</td>
<td width="75" >27</td>
</tr>
<tr id="totalRow">
<td>合计</td>
<td width="63" ></td>
<td width="68" ></td>
<td width="62" ></td>
<td width="75" ></td>
</tr>
</table>
I just learned the front-end. Does anyone know how to use JQ to automatically add up each column to the total?
There are many such tables on one page that need to be counted Q_Q
Pay attention to the HTML structure. You wrote
thead
but notbody
. Modify it as follows:Among them,
thead
andtbody
andtfoot
are all separated.thead
is the table header that is not traversed,tbody
is the traversal part, andtfoot
is the summary part, which is convenient for the jQuery selector.Update
Here are a few knowledge points:
new Array()
Construct a new array.fill()
fill arrayselector
:not()
and:first-child
Add
+
in front of a string or number to force it into a numberUsage of assignment operator
+=
Hope you can understand
html code
js code
jquery each traverses each column, adds them up and puts them in the last row