You need to enable JavaScript to run this app. 使用jQuery克隆thead元素后无法保持原有宽度的问题咨询
优惠活动
大模型
产品
解决方案
定价
更多

使用jQuery克隆thead元素后无法保持原有宽度的问题咨询

问题背景
  • 结合此前接触的前端表格实现示例产生疑问:难道克隆或复制thead元素后,最终无法得到与原thead宽度完全一致的副本?
  • 在另一个独立项目中复现了相同的问题:先创建新的thead,将原thead的HTML内容复制填充到克隆thead中,核心矛盾是两个thead的宽度始终无法匹配。
现有实现逻辑

代码的整体思路是逐列读取原thead对应元素的宽度,再把宽度值同步赋值给克隆thead的对应位置,具体代码如下:

var i = [];


var $original = $(this).find(".spec-table__thead.spec-table__thead--original th p");
// 读取原thead的元素宽度
$original.each(function(index) {
  i[index] = $(this).width();
});

var n = $(this).find(".spec-table__thead").html(),
  o = $('<thead class="spec-table__thead spec-table__thead--clone">' + n + '</thead>');

// 生成克隆thead的DOM对象
var $clone = $(this).find(".spec-table__thead--clone th p");
$("#spectablecontent").on('custom-update', '.spec-table__thead--clone', 
//  对应列的宽度打印逻辑目前为注释状态
//  function(index) {
//    console.log(i[index])
//  }
);

// 给克隆thead逐列匹配对应宽度
//  当前问题:无法正确获取原thead对应位置的宽度值
$clone.each(function(index) {
  console.log(i[index])
  $(this).width(i[index]);
});
当前卡点

给克隆thead逐列赋值宽度的环节,始终没法正确读取到原thead对应位置的宽度值,最终两个thead的列宽始终对不齐。


内容的提问来源于stack exchange,提问作者guradio

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.06 19:54:04