Jquery 向向右偏移的HTML表追加列
如何计算表的第一行中的列数,减去一,然后在该位置将列插入表中?我希望该列的第一行与其他行不同 问题是,它使用固定的、从左侧开始的位置来插入列。它也不会更改顶柱 下面是我从那篇文章改编的示例代码:Jquery 向向右偏移的HTML表追加列,jquery,html-table,sum,dynamic-columns,Jquery,Html Table,Sum,Dynamic Columns,如何计算表的第一行中的列数,减去一,然后在该位置将列插入表中?我希望该列的第一行与其他行不同 问题是,它使用固定的、从左侧开始的位置来插入列。它也不会更改顶柱 下面是我从那篇文章改编的示例代码: $(document).ready(function(){ $('#AddOT').click(function(){ $('#Table1').find('tr').each(function(){ $(this).find('td').eq(0).after(
$(document).ready(function(){
$('#AddOT').click(function(){
$('#Table1').find('tr').each(function(){
$(this).find('td').eq(0).after('<td>cell 1a</td>');
});
});
});
数列时有一个简单的步骤
编辑:问题解决了,我可以
这将非常有用,因为我在不同的行上有不同类别的文本输入框,并且我根据类别对它们求和。我现在可以动态地向所有行添加新列,但仍然在每行中保留唯一的单元格类。太棒了 我不确定我是否正确理解您的要求。看一看
$(文档).ready(函数(){
$('#AddOT')。单击(函数(){
var count=countColumn();
变量插入位置=计数-2;
$('#表1')。查找('tr')。每个(函数(索引){
如果(索引==0){
var colspan=$(this.attr('colspan')||“1”;
$(this.attr('colspan',parseInt(colspan)+1);
}
其他的
{
$(this).find('td').eq(insertedPosition).after('cell 1a');
}
});
});
});
函数countColumn(){
var colCount=0;
$('tr:nth child(1)td')。每个(函数(){
if($(this.attr('colspan')){
colCount++$(this.attr('colspan');
}否则{
colCount++;
}
});
返回colCount;
}
这肯定会将该列置于末尾!有没有办法把一号柱从末端移开?即第6/7列、第7/8列、第8/9列等?谢谢你的帮助:)@Shrout1:把count-1改成count-2。看看我更新的答案,这是一段漂亮的代码!非常感谢你帮助我-我昨天绞尽脑汁想把所有这些东西都绑在一起。
$(function() {
var colCount = 0;
$('tr:nth-child(1) td').each(function () {
if ($(this).attr('colspan')) {
colCount += +$(this).attr('colspan');
} else {
colCount++;
}
});
});
$(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('<td>cell 1a</td>');
}
});
});
});
function countColumn() {
var colCount = 0;
$('tr:nth-child(1) td').each(function () {
if ($(this).attr('colspan')) {
colCount += +$(this).attr('colspan');
} else {
colCount++;
}
});
return colCount;
}