【问题标题】:How do I set the height of every td element to it's width? [duplicate]如何将每个 td 元素的高度设置为其宽度? [复制]
【发布时间】:2015-02-03 20:33:17
【问题描述】:

我有一个宽度为 100% 的表格。它有七列,将宽度等分。我要做的就是让 td 元素的高度等于它的动态宽度。

有什么想法吗? jquery 很好

table { width: 100%;}

【问题讨论】:

    标签: javascript jquery css


    【解决方案1】:

    你可以试试下面的代码:

    $( window ).resize(function() {
      $('table>tr').height($('table>td').width());
    });
    

    但是。用 CSS 做这件事是击球手。因为使用 javascript 重绘是昂贵的计算。

    【讨论】:

    • Op 正在寻找单元格元素,而不是 TABLE 并且 btw 需要是动态的(我猜是响应式的)
    • @A.Wolff 抱歉,我更新了我的答案。
    【解决方案2】:

    试试...

    var w = $("table td").width() + "px";
    $("table td").css({ height: w });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-25
      • 2016-03-17
      • 1970-01-01
      • 2021-12-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多