创建<td>动态地添加到<tr>

Create <td> dynamically and add to <tr>

本文关键字:gt lt tr 添加 动态 td 创建      更新时间:2023-09-26

我有一个表:

  <table id="fTable">
    <tbody>
        <tr id="fRow">
        </tr>
    </tbody>
  </table>

我有一个有30列的网格,如何使用所有这些列来分隔<td>,并希望设置<td>的id、宽度、文本或innerHtml属性。

              var row = $('#fRow');
              for(var i= 2; i < Columns.length ; i++)
               {
               }

正如我所想(以及评论)。。。您是使用jquery的新手。。。所以使用jquery非常容易。。。append/appendTo就是您要查找的。。。

如果您想将TD添加到多个表中,那么使用ID属性是没有用的。因为W3C说ID在页面上是唯一的。。。更好地使用class属性。。。

<table class="floatTable">
    <tbody>
        <tr class="footerRow">
        </tr>
    </tbody>
  </table>
// Select all TRs in the floatTable having the class footerRaw
$('.floatTable tr.footerRaw').each(function(key, el)) {
  // here you could define anything whatever you want
  var tdContent = 'Lorem ipsum dolor';
  // For example add five TDs to your table
  for ( var i = 0; i < 5; i++ ) {
    // if it works ;-)
    // ...it should add following:
    // <td>Lorem ipsum dolor #1</td>
    // <td>Lorem ipsum dolor #2</td>
    // ...and so on...
    $(this).append('<td>' + tdContent + ' #' + i + '</td>');
  }
});

下面是一个运行示例。。。http://jsfiddle.net/2am6wcm8/