Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jquery编辑表中特定行的colspan属性_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 使用jquery编辑表中特定行的colspan属性

Javascript 使用jquery编辑表中特定行的colspan属性,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我的页面中有一个HTML表。像这样 $('tr').each(function(index, tr) { $(this).find('td:not(:empty)').attr('colspan', $(this).find('td:empty').length); $(this).find('td:empty').remove(); }); 不 选择权 猛冲 嗯 英寸 嗯 嗯 19904408 S/G -4 4.4 5/16" 7.9 20 材质:耐酸不锈钢,镶青铜螺纹

我的页面中有一个HTML表。像这样

 $('tr').each(function(index, tr) {
    $(this).find('td:not(:empty)').attr('colspan', $(this).find('td:empty').length);
    $(this).find('td:empty').remove();
});

不
选择权
猛冲
嗯
英寸
嗯
嗯
19904408
S/G
-4
4.4
5/16"
7.9
20
材质:耐酸不锈钢,镶青铜螺纹。
-K=带螺母的U形夹销,G=带G形环的U形夹销

只是给出一个想法。你可以试试这个。适用于下一个空元素

    $('tr').each(function(index, tr) {
    var previous = false;
    $(tr).find('td').each(function(index1, td) {
        if(td.innerText == "") {
            if(previous) {
                var p = $(previous);
                if(!p.attr('colspan')) {
                    p.attr('colspan', 1);
                }
                p.attr('colspan', parseInt(p.attr('colspan'))+1);
                $(td).remove();
            }
        } else {
            previous = td;
        }
    });
});
你也可以这样写

 $('tr').each(function(index, tr) {
    $(this).find('td:not(:empty)').attr('colspan', $(this).find('td:empty').length);
    $(this).find('td:empty').remove();
});
对于拆分单元格长度,可以更改第二行

$(this).find('td:not(:empty)').attr('colspan', $(this).find('td:empty').length/$(this).find('td:not(:empty)'));

检查子单元格是否为空(如果为空),删除它们并增加上一个单元格colspan@raghavendra你能用必要的代码示例将其添加为答案吗?在本例中,它只检查下一个单元格。你能解释一下我是如何做到这一点的吗。“检查除第一个单元格外的所有单元格都是空的。”“@Athul您使用的是第1个或第2个解决方案?@Athul您可以使用slice对其执行“slice to it”,我不明白。$(this).find('td:empty')。slice(1)将跳过tr中的第一个td,以便这样使用