我制作了一个按钮来将单元格作为列添加到一组行中,但是当我尝试单击该按钮来添加整个列时,它会插入 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>')
}
})
最佳答案
如果你使用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/