日期:2014-05-16  浏览次数:20355 次

js计算总和
<script>
Google={
$:/* 简写getID */function (x){
return typeof x=='string'?document.getElementById(x):x
}
,bind:/* 绑定Table */function(x){
this.cells=this.$(x)?this.$(x).getElementsByTagName("TD"):[];
}
,get:/* 按列号返回运算和 */function(n){
if(/\D/.test(n))return 0;
for (var i=0,sum=0;i<this.cells.length;i++) {
var x=this.cells[i];
if(/* 列号相同 */x.cellIndex==n)
sum+=(parseInt(x.innerHTML)||0);
}
return sum;
}
};
window.onload=function (){
Google.bind('dhooo');
Google.$('colNum').onkeyup=/* 键入事件 */function (){
Google.$('sumShow').innerHTML=/* 显示 */Google.get(this.value);
}
}
</script>
<style type="text/css">
body,td{font-size:12px;}
strong{color:red; }
td{width:100px;text-align:center}
</style>
<body>
<table cellspacing="0" cellpadding="0" border="1" id="dhooo">
<tr><td>MM-song</td><td>85</td><td>14</td><td>25</td></tr>
<tr><td>15</td><td>7</td><td>15</td><td>90</td></tr>
<tr><td>51</td><td>255</td><td>15</td><td>105</td></tr>
<tr><td>10</td><td>115</td><td>70</td><td>15</td></tr>
</table>
<br/>
第 <input type="text" id="colNum" size="4" /> 列的和等于
<strong id="sumShow"></strong>
</body>