<table>
<tr>
<td id="r1c1">
</td>
<td id="r1c2">
</td>
</tr>
<tr>
<td id="r2c1">
</td>
<td id="r2c2">
</td>
</tr>
</table>
<script>
function sum()
{
    var temp = parseInt(r1c1.innerText) + innerText.parseInt(r1c2.innerText);
}
</script>这是求和,其他雷同

解决方案 »

  1.   

    sorryvar temp = parseInt(r1c1.innerText) + innerText.parseInt(r1c2.innerText);改为:
    var temp = parseInt(r1c1.innerText) + parseInt(r1c2.innerText);
      

  2.   

    <html><head><title>test</title></head>
    <body>
    备注:应将百分比的文本框设为只读
    NaN表示无效数值,请自己决定如何处置<table border="1" height="144" width="494" id="tab1">
    <tr rowspan=3 align=center class=tr1>    
    <td  width="50" height="24" align="center">&nbsp;</td>    
    <td  width="70" height="24" align="center">抽样体重</td>    
    <td  width="59" height="24" align="center">公数量</td>    
    <td  width="53" height="24" align="center" bgcolor="#C0C0C0">公比例</td>    
    <td  width="51" height="24" align="center">母数量</td>    
    <td  width="54" height="24" align="center" bgcolor="#C0C0C0">母比例</td>    
    <td  width="54" height="24" align="center">幼数量</td>    
    <td  width="52" height="24" align="center" bordercolor="#000000" bgcolor="#C0C0C0">幼比例</td>    
    </tr>
    <tr align=center>    
    <td width="50" height="23">猪</td>    
    <td width="70" height="23"><input type=text name="num500" size=8 ></td>    
    <td width="59" height="23"><input type=text name="num220" size=7 ></td>    
    <td width="53" height="23"><input type=text name="dj1num" size=6 readonly></td>    
    <td width="51" height="23"><input type=text name="dj1num" size=6></td>    
    <td width="54" height="23"><input type=text name="dj1num" size=6 readonly></td>    
    <td width="54" height="23"><input type=text name="dj1num" size=6></td>    
    <td width="52" height="23"><input type=text name="dj1num" size=6 readonly></td>    
    </tr>
    <tr align=center>    
    <td width="50" height="23">狗</td>    
    <td width="70" height="23"><input type=text name="num500" size=8 ></td>    
    <td width="59" height="23"><input type=text name="num220" size=7 ></td>    
    <td width="53" height="23"><input type=text name="dj1num" size=6 readonly></td>    
    <td width="51" height="23"><input type=text name="dj1num" size=6></td>    
    <td width="54" height="23"><input type=text name="dj1num" size=6 readonly></td>    
    <td width="54" height="23"><input type=text name="dj1num" size=6></td>    
    <td width="52" height="23"><input type=text name="dj1num" size=6 readonly></td>    
    </tr>
    <tr align=center>    
    <td width="50" height="23">羊</td>    
    <td width="70" height="23"><input type=text name="num500" size=8 ></td>    
    <td width="59" height="23"><input type=text name="num220" size=7 ></td>    
    <td width="53" height="23"><input type=text name="dj1num" size=6 readonly></td>    
    <td width="51" height="23"><input type=text name="dj1num" size=6></td>    
    <td width="54" height="23"><input type=text name="dj1num" size=6 readonly></td>    
    <td width="54" height="23"><input type=text name="dj1num" size=6></td>    
    <td width="52" height="23"><input type=text name="dj1num" size=6 readonly></td>    
    </tr>
    <tr align=center>    
    <td width="50" height="21"><input type=button value=合计 onclick="cal()"></td>    
    <td width="70" height="21">&nbsp;</td>    
    <td width="59" height="21">&nbsp;</td>    
    <td width="53" height="21">&nbsp;</td>    
    <td width="51" height="21">&nbsp;</td>    
    <td width="54" height="21">&nbsp;</td>    
    <td width="54" height="21">&nbsp;</td>    
    <td width="52" height="21">&nbsp;</td>    
    </tr>
    </table><script>
    function toNum(str){
      n = parseInt(str);
     if(isNaN(n)) return 0;
     else return n ;
    }function calRow(n){ // 计算百分比
          // 输入值不是数字时作为0处理
         var row = tab1.rows(n);
     var male=  toNum(row.cells(2).firstChild.value);
     var female = toNum(row.cells(4).firstChild.value);
     var child =  toNum(row.cells(6).firstChild.value);
         var s = male + female + child;
         row.cells(3).firstChild.value = Math.round(100*male/s) + "%";
         row.cells(5).firstChild.value = Math.round(100*female/s) + "%";
         row.cells(7).firstChild.value = Math.round(100*child/s) + "%";
    }function calCol(n){ //计算各列之和
      var s=0;
      if(n==3 || n==5 || n==7) return; // 百分比不能简单累加
      for(i=1;i<4;i++)
         s += toNum(tab1.rows(i).cells(n).firstChild.value);
      tab1.rows(4).cells(n).innerText = s;
    }function cal(){
     for(i=1;i<4;i++) calRow(i);
     for(j=1;j<=7; j++) calCol(j);
    }
    </script>
    </body>
    </html>
      

  3.   

    非常感谢两位的热心解答,但当我在表头加上一行
    <tr><td colspan=3>pp</td>
        <td colspan=3>ww</td>
        <td colspan=2>ee</td></tr>
    时就报错了,本人初学JS,对其属性了解很少,但这次业务又少不了,还望大家
    热心帮忙!(以上这个<tr>是一定要有的!)
    ------------------------------------------------
    另外,: 感谢onestab(┼─)的详细解答,但我的意思是按“合计”按钮后,
    对各比例求平均值(是直接输入的)后显示在合计行内,就是除了“合计”这
    一行外都不涉及计算。是我没说清楚/
      

  4.   

    1。表头应该事先设计好,不应随意改变。
    2。表头的设计似乎有问题:
    <tr><td colspan=3>pp</td> ???为什么把前三列合并?
        <td colspan=3>ww</td> ???
        <td colspan=2>ee</td></tr> ???
    请决定了以后再说。我的意见:
    <tr><td colspan=1></td>
        <td colspan=2>公</td>
        <td colspan=2>母</td>
        <td colspan=2>幼</td></tr>
    3。幼畜就不分公母了吗?:) 你详细研究一下我的代码,应该可以自己解决的.
      

  5.   

    我这只是一个测试表格,非常感谢你的回应!
    问最后一个问题,firstChild属性是什么含义?另外百分比显示为00.00%用什么函数呢?
    -----------------------------
    另外,我有个相同的(20分)问题,希望你去领!
      

  6.   

    obj.firstChild是obj元素所包含的第一个子元素,因为你的td中只有一个input,所以也就是这个input。
    百分比的显示没有用什么函数。
    比如: 9/18 = 0.5
    转换成百分比字串:str =  100*9/18 + "%"
    另外需注意,表格的行(rows)和单元格(cells)集合的元素都是以0开始的。