javascript - 如何使用 jQuery 构建一个完整的 HTML 表格?

标签 javascript jquery

我希望使用 jQuery 构建一个完整的表格。

我尝试从这个问题中学习或模仿

Create table with jQuery - append

但运气不好。

我想要创建的是

<table>
   <thead>
      <tr>                                  
         <th>Problem 5</th>
         <th>Problem 6</th>
      </tr>
   </thead>
   <tbody>
      <tr>
         <td>1</td>
         <td>2</td>
      </tr>
   </tbody>
</table>

我创建的 jQuery 代码是

var $table = $('<table/>');

var $thead = $('<thead/>');
$thead.append('<tr>' + '<th>Problem 5</th>' + '<th>Problem 6</th>' + '</tr>';
$table.append(thead);

var $tbody = $('<tbody/>');
$tbody.append( '<tr><td>1</td><td>2</td></tr>' );
$table.append(tbody);

$('#GroupTable').append($table);

但是运行失败。

谁能告诉我为什么?

最佳答案

您在 $thead 声明中缺少右括号:

$thead.append('<tr>' + '<th>Problem 5</th>' + '<th>Problem 6</th>' + '</tr>'); 
                                                                            ^

并且您没有正确附加变量:

$table.append($thead);
              ^

$table.append($tbody);
              ^

这是一个working fiddle .

关于javascript - 如何使用 jQuery 构建一个完整的 HTML 表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10888925/

相关文章:

ipad 上的 JQuery focus() 方法

javascript - 如何在 jQuery 中仅检查所有类别复选框或其他剩余类别复选框

javascript - 使用 JQuery 预填充选择字段的下拉选项验证

javascript - 向现有对象添加属性

javascript - 剑道网格中总和列的总和

javascript - 如何将子菜单添加到单击的按钮

javascript - Vue - 如何将父引用作为 Prop 传递给子?

javascript - 如果通过 Google 跟踪代码管理器自定义 HTML 将 JavaScript 代码插入页面,它们的延迟程度如何?

javascript - jquery - 尽管条件不满足,为什么 if 子句会触发?

javascript - 当使用append()更新值时,需要文本区域的提交被阻止