使用 jQuery 将 td 附加到表中的 tr

Append td to tr in table with jQuery

本文关键字:tr jQuery td 使用      更新时间:2023-09-26

我在工作中学到了一种比以前使用的方法更全面的方法(见这里)。我发现自己有下面的代码。我想将最后一个 td (col2) 添加到父 tr 中,而不是上一个 td (col1)。如何进行?

到处找,都没有找到我问题的答案......

$('.main').append(
  $('<div>', {'class' : 'box'}).append(
    $('<div>', {'class' : 'table_responsive'}).append(
      $('<table>', {'class' : 'table'}).append(
        $('<tbody>').append(
          $('<tr>').append(
            $('<td>', {'class' : 'text-center', 'text' : 'col1'}).append(
            $('<td>', {'class' : 'text-center', 'text' : 'col2'})
            )
          )
        )
      )
    )
  )
);

你只需要移动.append()来遵循tr声明,而不是td

$('<tr>')
    .append($('<td>', {'class' : 'text-center', 'text' : 'col1'}))
    .append($('<td>', {'class' : 'text-center', 'text' : 'col2'}))