猿问

如下图片+代码,请问JS如何实现获取表格中的单元格里面的数据,并让其累加呢?

<script type="text/javascript" >
      var getTd=document.getElementById("myTable");
          rowsLength=getTd.rows.length;      var theDate ;    for(var i=1;i<rowsLength;i++)
    {	   var std=getTd.rows[i].cells[2].innerText;	   var stdVlue=std.replace(/天/gi,'');
	    stdVlue=Number(stdVlue);	//getTd.rows[i].cells[11].innerText=stdVlue;
	      for(var g=rowsLength;g>=2;g--)
	    {	        var std2=getTd.rows[g].cells[11].innerText;	        var stdVlue2=std2.replace(/天/gi,'');
	        stdVlue2=Number(stdVlue2);
	        theDate=stdVlue2+stdVlue;
	    }
   	    getTd.rows[i].cells[11].innerText=stdVlue;        document.write(stdVlue);        document.write("<br/>");        document.write(theDate);        document.write("<br/>");

    }</script>
喵喔喔
浏览 113回答 1
1回答

森栏

其实 ... 我没太弄明白你的程序要实现什么效果 ...所以只好按照标题来回答你 ... 如何累加表格中单元格的数据 ...写了一个简单的程序来作为示例 ... 如下 ...<table&nbsp;id="thetable"&nbsp;border="1"&nbsp;cellspacing="1"&nbsp;cellpadding="7"&nbsp;width="50%"> &nbsp;&nbsp;&nbsp;&nbsp;<caption>Sunyanzi's&nbsp;Test</caption> &nbsp;&nbsp;&nbsp;&nbsp;<tbody> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<tr><td>1</td><td>2</td></tr> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<tr><td>3</td><td>4</td></tr> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<tr><td>5</td><td>6</td></tr> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<tr><td>7</td><td>8</td></tr> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<tr><td>9</td><td>10</td></tr> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<!--&nbsp;the&nbsp;row&nbsp;below&nbsp;is&nbsp;used&nbsp;to&nbsp;fill&nbsp;sum&nbsp;data&nbsp;...&nbsp;--> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<tr><th></th><th></th></tr> &nbsp;&nbsp;&nbsp;&nbsp;</tbody></table><script&nbsp;type="text/javascript"> &nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;get&nbsp;our&nbsp;table&nbsp;...&nbsp;*/ &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;table&nbsp;=&nbsp;document.getElementById(&nbsp;"thetable"&nbsp;);&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;last&nbsp;line&nbsp;not&nbsp;contain&nbsp;data&nbsp;...&nbsp;kick&nbsp;it&nbsp;out&nbsp;...&nbsp;*/ &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;totalRows&nbsp;=&nbsp;table.rows.length&nbsp;-1;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;initialize&nbsp;two&nbsp;variables&nbsp;as&nbsp;basket&nbsp;...&nbsp;*/ &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;firstColumn&nbsp;=&nbsp;secondColumn&nbsp;=&nbsp;0;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;traverses&nbsp;from&nbsp;first&nbsp;row&nbsp;to&nbsp;last&nbsp;...&nbsp;*/ &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(&nbsp;var&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;<&nbsp;totalRows;&nbsp;++&nbsp;i&nbsp;)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;add&nbsp;new&nbsp;value&nbsp;to&nbsp;our&nbsp;basket&nbsp;...&nbsp;*/ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;firstColumn&nbsp;+=&nbsp;Number(&nbsp;table.rows[i].cells[0].innerHTML&nbsp;)&nbsp;||&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;secondColumn&nbsp;+=&nbsp;Number(&nbsp;table.rows[i].cells[1].innerHTML&nbsp;)&nbsp;||&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;put&nbsp;the&nbsp;result&nbsp;in&nbsp;last&nbsp;row&nbsp;...&nbsp;*/ &nbsp;&nbsp;&nbsp;&nbsp;table.rows[i].cells[0].innerHTML&nbsp;=&nbsp;firstColumn; &nbsp;&nbsp;&nbsp;&nbsp;table.rows[i].cells[1].innerHTML&nbsp;=&nbsp;secondColumn;</script>顺便提一下你的代码里面的几个小问题 ... 希望能对你有些帮助 ...首先是表格中的&nbsp;rows&nbsp;和&nbsp;cells&nbsp;数组 ... 第一个元素的索引都是&nbsp;0&nbsp;而不是 1 ...比如表格的第一行 ... 应该是&nbsp;i = 0&nbsp;而不是&nbsp;i = 1&nbsp;...第一列和第二列 ... 应该是&nbsp;cells[0]&nbsp;和&nbsp;cells[1]&nbsp;而不是&nbsp;cells[1]&nbsp;和&nbsp;cells[2]&nbsp;...以及你用到了 innerText ... 在 Gecko 内核的浏览器上是没有这个属性的 ...Gecko 上类似的属性叫 textContent ... 当然其他浏览器也不支持 ...所以当你的&nbsp;td&nbsp;标签中只包含数字不包含其他 HTML 标签的时候 ...完全可以用兼容性更好的&nbsp;innerHTML&nbsp;来处理 ...基本上我一眼能看到的就这些 ...我觉得既然你能写到这里 ... 知道问题所在绝对就可以把程序改好了 ...恩加油~
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答