jquery写表格 手动合并单元格

该篇文章展示了如何使用HTML和jQuery构建一个带有表头合并功能的表格,通过JavaScript动态加载数据并填充到表格中。
摘要由CSDN通过智能技术生成
<!DOCTYPE html>
<html>

<head>
  <style>
    .special-row th:first-child,
    .special-row th:nth-child(2) {
      background-color: yellow;
      text-align: center;
    }
  </style>
</head>

<body>
  <div id="tableWrapper"> <!-- 添加包裹表格的容器 -->
    <table id="table">
      <thead>
        <tr>
          <th>日期</th>
          <th>姓名</th>
          <th>地址</th>
        </tr>
        <tr class="special-row">
          <th colspan="2">表头合并单元格</th>
        </tr>
      </thead>
      <tbody id="tableBody">
      </tbody>
    </table>
  </div>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script>
    var tableData = [
      {
        date: '2016-05-02',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1518 弄'
      },
      {
        date: '2016-05-01',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1519 弄'
      },
      {
        date: '2016-05-03',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1516 弄'
      },
      {
        date: '2016-05-03',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1517 弄'
      }
    ]

    $(document).ready(function () {
      var $tableBody = $('#tableBody')
      var $tableHead = $('#table')
      $.each(tableData, function (index, item) {
        var $row = $('<tr>')
        $row.append($('<td>').text(item.date))
        $row.append($('<td>').text(item.name))
        $row.append($('<td>').text(item.address))
        $tableBody.append($row)

      })
    });
  </script>
</body>

</html>

效果如下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值