gpt4 book ai didi

javascript - 将 td 元素插入到行组中

转载 作者:行者123 更新时间:2023-12-02 22:27:58 25 4
gpt4 key购买 nike

我制作了一个按钮来将单元格作为列添加到一组行中,但是当我尝试单击该按钮来添加整个列时,它会插入 td元素作为文本

这是代码

add_col.on('click',function() {
var table = $('#table_data'), //get the table
rowsNum = $('#table_data tr').length // get number of rows
cellsNum = document.getElementById('table_data').rows[0].cells.length
for(x = 0;x < rowsNum; x++){
//document.getElementById('table_data').rows[x].insertCell(cellsNum)
document.getElementById('table_data').rows[x].append('<td></td>')
}
})

[https://jsfiddle.net/georgesteven1/3kpg6e8c/9/ ][1]

最佳答案

如果你使用Jquery,你可以遍历表tr并添加一个新的td。

add_col.on('click',function() {
$('#table_data tbody tr').each(function( index ) {
$('<td />').appendto($(this));
});
});

关于javascript - 将 td 元素插入到行组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58995350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com