猿问

如何在不限定表格行数和列数的情况下进行列求和?

问题

表格的行数与列数不固定,在 tfoot 中需要对 tbody 的列进行求和计算。如何动态的求和?

需求

例如 tfoot 的第二列为 tbody 的第二列之和,以此类推,行数、列数不固定

MMTTMM
浏览 893回答 1
1回答

偶然的你

ABC123456789这个是随便写了一下的table表<script type="text/javascript">&nbsp; &nbsp; $(function(){&nbsp; &nbsp; &nbsp; &nbsp; $('tfoot td').each(function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var td_index = $(this).index();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var _total = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('tbody tr').each(function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var _num = parseInt($(this).find('td').eq(td_index).text());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _total += _num;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(this).text(_total);&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; })</script>这个是jquery.写得有点乱。。功能是可以实现的。你可以看下。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答