$.fn.extend({ tableColumnSum:function (cols,cells) { if(typeof(cols) === 'string'){ cols = cols.split(','); } cells = cells || cols; if(typeof(cells) === 'string'){ cells = cells.split(','); } var $tr = $(this).find('tr'); var sum = []; for (var i=0;i<cols.length;i++){ sum.push(0); } $.each($tr,function (i,v){ for(var j=0;j<cols.length;j++){ sum[j] += Number($(v).children('td:eq('+cols[j]+')').text()); } }); var $tfood = $(this).next().find('tr'); $.each(cells,function (i,v) { $tfood.children('th:eq('+v+')').text(sum[i]); }); return $(this); } });
调用:
$('table tbody').tableColumnSum('8,9')