<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Converting ISO 8601 date</title>
<style>
#box{
position:absolute;
left:100px;
top: 100px;
width:200px;height:200px;
background-color:mediumseagreen;
}
</style>
</head>
<body>
<table id="table1">
<tr>
<td>Washington</td><td>145</td>
</tr>
<tr>
<td>Oregon</td><td>233</td>
</tr>
<tr>
<td>Missouri</td><td>833</td>
</tr>
</table>
<script type="text/javascript">
var result=document.querySelectorAll("td:nth-child(2)");//特别注意此处对选择器属性的运用。
var sum=0; for(var i=0;i<result.length;i++){ sum+=parseFloat(result[i].firstChild.data);// 注意提取table中值用data属性,转化成数值,需要parseFloat/parseInt方法。 } var newRow=document.createElement("tr");var firstCell=document.createElement("td"); firstCell.appendChild(document.createTextNode("sum:"));newRow.appendChild(firstCell); var secondCell=document.createElement("td"); secondCell.appendChild(document.createTextNode(sum));newRow.appendChild(secondCell); document.getElementById("table1").appendChild(newRow);</script></body></html>
1.5 提取单元格数值,并累加 P34
最新推荐文章于 2024-07-06 16:47:02 发布