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>
相关文章推荐
- The matching wildcard is strict, but no declaration can be found for element 解决方法
- Shell脚本-判断文件有无进而复制
- NYOJ 257 郁闷的C小加(一) (栈 、中缀转后缀)
- 拯救小矮人(codevs 2544)
- QEMU/KVM虚拟机安装配置
- 【机器学习】Logistic Regression 学习笔记
- 深入了解JavaScriptCore(二)
- 2016/7/6
- 【jsp】自定义jsp标签(下拉框数据字典)
- 用nodejs做Web前端服务器的一些经验
- 这家美妆类短视频社区电商是如何解决发展瓶颈的?
- 利用Python将Linux conf配置文件的内容解析成字典格式
- 利用Python将Linux conf配置文件的内容解析成字典格式
- 匠人精神
- android studio配置git
- RecyclerView基本使用(二)-- 返回不同类型的View
- leetcode227. Basic Calculator II
- 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告
- centos下安装xgboost
- Swift2.0:数据类型笔记