您的位置:首页 > 其它

1.5 提取单元格数值,并累加 P34

2016-07-06 17:56 162 查看
<!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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: