日期:2014-05-16 浏览次数:20402 次
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
  <title>无标题页</title>
  <script type="text/javascript">
    function test(th) {
      table = th.parentNode;
      while (table.tagName != "TABLE")
        table = table.parentNode;
      for (i = 1; i < table.rows.length; i++) {
        table.rows[i].cells[2].innerHTML = parseFloat(table.rows[i].cells[0].innerHTML) * parseFloat(table.rows[i].cells[1].innerHTML);
      }
    }
  </script>
</head>
<body>
  <form id="form1" runat="server">
<table>
<thead>
<tr>   
  <th type="tc" name="price" onclick="test(this)">单价(元)</th>
  <th type="tc" name="num">数量</th>
  <th type="tx" name="totalprice">总价(元)</th>
</tr>
</thead>
<tbody>
   <tr>
   <td>33</td>
   <td>2</td>
   <td></td>
   </tr>
   <tr>
   <td>12</td>
   <td>5</td>
    <td></td>
   </tr>
</tbody>
   
  </table>
  </form>
</body>
</html>
------解决方案--------------------
function test(o){
    var p=o.parentNode.getElementsByTagName("th");
    var totalprice=1;
    for(i=0;i<p.length;i++){
        if(p[i].getAttribute("type")=='tc'){
            totalprice*=parseFloat(p[i].innerHTML);
        }else{
            p[i].innerHTML=totalprice;
        }
    }
}