简体   繁体   English

如何使用jQuery找到最大的表格单元格高度?

[英]How can I find the largest table cell height with jQuery?

I have a table with cells of variable-length text-content. 我有一个表格,其中包含可变长度文本内容的单元格。 I want to find the height of the tallest cell and then make all the cells that height. 我想找到最高单元格的高度,然后使所有单元格都达到该高度。 How can I do this? 我怎样才能做到这一点?

Like this: 像这样:

var max = 0;    
$('table td').each(function() {
    max = Math.max($(this).height(), max);
}).height(max);

In plain english, loop through all the cells and find the maximum value, then apply that value to all the cells. 用简单的英语,遍历所有单元格并找到最大值,然后将该值应用于所有单元格。

Just to be different: 只是有所不同:

var heights = $('td').map(function() {
    return $(this).height();
}).get();

var maxHeight = Math.max.apply(Math, heights);

$('td').css('height', maxHeight);

There are several plugins that can do this for you. 几个插件可以为您完成此任务。 However, the code would look like this: 但是,代码如下所示:

var tallest = 0;
$('table td').each(function() {
  if (tallest < $(this).height()) {
    tallest = $(this).height();
  }
});

$('table td').css({'height': tallest});

contribute @tatu ulmanen's example 贡献@tatu ulmanen的例子

This is obvious but if you just want to strech the rows with taller cells wrap a tr loop :) 这很明显,但是如果您只想拉高单元格的行,则可以包裹一个tr循环:)

$('table tr').each(function (){
    var max = 0;
    $(this).find('td').each(function (){
        max = Math.max($(this).height(), max);
    }).height(max);
});

You can use jQuery with the following code 您可以将jQuery与以下代码结合使用


var maxHeight = 0;
$('#yourTableID td').each(function(index, value)
{
if ($(value).height() > maxHeight)
maxHeight = $(value.height());
}).each(function(index, value) {
$(value).height() = maxHeight;
});

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM