将列附加到从右侧偏移的HTML表

如何计算表格第一行中的列数,减1,然后在该位置插入一列到我的表中? 我希望该列的第一行与其他列不同。

这是一个基本的小提琴,它将列插入到固定位置的表中。

本主题讨论插入新列。 问题是它使用从左侧固定的位置来插入列。 它也不会更改顶部列。

这是我从该post改编的示例代码:

$(document).ready(function(){ $('#AddOT').click(function(){ $('#Table1').find('tr').each(function(){ $(this).find('td').eq(0).after('cell 1a'); }); }); }); 

本主题讨论确定“表”中的列数。 该示例似乎不起作用,因为在HTML中放置多个表会产生问题:

  $(function() { var colCount = 0; $('tr:nth-child(1) td').each(function () { if ($(this).attr('colspan')) { colCount += +$(this).attr('colspan'); } else { colCount++; } }); }); 

有一个jsfiddle与列的计数一起。

编辑:问题解决了,我能够根据行索引值制作一个修改的小提琴,将不同的内容放入不同的行。

这将非常有用,因为我在不同的行上有不同类别的文本输入框,我根据类对它们求和。 我现在可以动态地向所有行添加新列,但仍然在每行中保留唯一的单元类。 真棒!

我不确定我是否正确理解你的要求。 看看演示

 $(document).ready(function(){ $('#AddOT').click(function(){ var count = countColumn(); var insertedPosition = count-2; $('#Table1').find('tr').each(function(index){ if (index == 0){ var colspan = $(this).attr('colspan') || "1"; $(this).attr('colspan',parseInt(colspan)+1); } else { $(this).find('td').eq(insertedPosition ).after('cell 1a'); } }); }); }); function countColumn() { var colCount = 0; $('tr:nth-child(1) td').each(function () { if ($(this).attr('colspan')) { colCount += +$(this).attr('colspan'); } else { colCount++; } }); return colCount; }