【问题标题】:cell width attribute not copied correctly when border-collapse边框折叠时未正确复制单元格宽度属性
【发布时间】:2014-11-02 22:29:04
【问题描述】:

我通过复制 tr html 然后复制 th 宽度来克隆表标题行。但这失败了,因为当边框:'Wpx' 和边框折叠:'collapse' 时,宽度被写为(宽度减去 W)像素。如果没有设置border-collapse,则宽度写入正确。

代码很简单:

var arr = [];

// copy html, where abc is the destination <tr> and def is the source
$abc.html($def.html());

// now copy widths of table header cells 
$def.children().each(function()  {arr.push($(this).css("width"));});
$abc.children().each(function(index) {$(this).css("width", arr[index]);});

Fiddle 显示问题:http://jsfiddle.net/marvmartian/9z24j7vz/4/

一种解决方法(在小提琴中说明,但已注释掉)是检查最终表格的大小,如果它们不相等,(hacky)在​​编写它们之前将 W 添加到宽度。

这种行为似乎是一个错误(使用 Chrome)。显然这是一个特点。我在这里错过了什么?

【问题讨论】:

  • 我看不到问题,实际上,在 Firefox 中,但我在 chrome 中看到它......(win7,最新版本的浏览器)
  • 脑抽筋,我在 FF 中也看不到。对不起。

标签: javascript jquery css


【解决方案1】:

而且,这里是修复:

table.pvtTable tr th {
    background-color: #e6EEEE;
    border: 6px solid black;
    padding: 5px;
    box-sizing:border-box;
}

Chrome 不会将单元格宽度计算为宽度 + th 边框宽度(我猜是因为边框折叠,在父元素上,我猜?),如果你在 css 中添加它 - 边框包含在最终宽度中。但是,这次不确定哪个(firefox 或 chrome)是正确的。 :)

小提琴:http://jsfiddle.net/9z24j7vz/5/

【讨论】:

  • 事实证明这毕竟不是一个普遍的答案。准确地做到这一点的唯一方法是将
    放在单元格中并设置 div 大小,如stackoverflow.com/questions/13280798/… 中所述。正如该线程的一位评论者所指出的那样,getComputedStyle() 在某些情况下似乎也有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-08
  • 1970-01-01
  • 2010-10-10
  • 2012-11-07
  • 2023-03-11
  • 1970-01-01
相关资源
最近更新 更多