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

一段js的实现
[code=HTML][/code]
  <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>
   
  </tbody>
   
  </table>
在方法test(this)中 如何实现 把 类型 [type=tc] 的值 相乘,所得到的值赋给 [type="tx"] ,其中type属性是我自定义的。 [type=tc] * [type=tc] * ....*[type=tc] =[type=tx] 就是这么个意思。

------解决方案--------------------
你是要这样的效果吧
HTML code

<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>

------解决方案--------------------
JScript code
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;
        }
    }
}