jquery - 克隆 tr,但保留宽度

标签 jquery html html-table width clone

我正在尝试克隆表中的一行,并将该行插入到空表中。简单。

但是是否可以保留 tr 中单元格的原始宽度(可能会由于同一列中的其他单元格而扩展)?

编辑刚刚意识到我应该在克隆后迭代匹配的单元格并同步宽度。仍然好奇是否有更直接的方法......

最佳答案

克隆行,然后迭代该行的子行,将每个子行的宽度设置为相应索引处原始行的每个子行。

var target = $('#target');
var target_children = target.children();

var clone = target.clone();

clone.children().width(function(i,val) {
    return target_children.eq(i).width();
});

$('<table>',{border:1}).append(clone).appendTo('body');

示例: http://jsfiddle.net/z7hWh/

我不确定浏览器是否支持在<td>上设置宽度.

关于jquery - 克隆 tr,但保留宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6539489/

相关文章:

Jquery zoom with a clickable element that doesn't disrup the functionality

jquery - 如何分别克隆父级的每个 div

javascript - 向 Jquery 中的元素添加自定义属性

html - 将行添加到 webbrowser 对象中的现有表

html - 从电子邮件提交表单

php - 从数据表按钮中的导出中排除列

当表为空时 JavaScript 禁用按钮

javascript - Td .innerHTML 在循环子级时不显示

html - CSS 问题,表格未填满屏幕

javascript - mouseenter/leave img to fadeIn/Out text div 不工作