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

标签 javascript jquery html

我制作了一个按钮来将单元格作为列添加到一组行中,但是当我尝试单击该按钮来添加整个列时,它会插入 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/

相关文章:

jquery - 在加载其他所有内容后加载 Google map

html - 刷新后保持选中选项

javascript - DHTMLX 调度程序与 Bootstrap 冲突

javascript - 你如何组织你的 Javascript 代码?

jquery - 从变量中移动具有相同源名称的 img

html - Bootstrap 旋转木马 500 x 500

html - SVG 在 IE9 中无法正常工作

javascript - $ajax 调用在 chrome/safari 下载文件期间挂起,而不是在 IE/firefox 中

javascript - 在文本字段中突出显示实时语法?

javascript - 为什么没有在页面源中添加元素