【问题标题】:jQuery table width of titlejQuery表格标题的宽度
【发布时间】:2012-09-23 04:28:03
【问题描述】:

我有一个 table,标题短但单元格长 (td)。 是否可以将列宽度设为标题的最小宽度?

例子:

<table>
    <tr>
        <th>Name</th>
        <th>phone</th>
    </tr>
    <tr>
        <td>Santa</td>
        <td>20938570987098709870298349082456</td>
    </tr>
</table>

所以,“电话”列应该显示“20.....”

有可能吗?

【问题讨论】:

标签: jquery html-table width col


【解决方案1】:

如果你需要精确的宽度并且你没有使用等宽字体,那就更难了。

$("th").each(function () {
   var chars = $(this).text().length;
   $("td").eq($(this).index()).each(function () {
      if ($(this).text().length > chars) {
         $(this).text($(this).text().substring(0, chars) + '...');
      }
   });
});

未经测试或优化;只是一个概念验证。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-22
    • 1970-01-01
    • 2015-03-21
    • 2012-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多