繁体   English   中英

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

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

我有一个表格,其中包含可变长度文本内容的单元格。 我想找到最高单元格的高度,然后使所有单元格都达到该高度。 我怎样才能做到这一点?

像这样:

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

用简单的英语,遍历所有单元格并找到最大值,然后将该值应用于所有单元格。

只是有所不同:

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

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

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

几个插件可以为您完成此任务。 但是,代码如下所示:

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

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

贡献@tatu ulmanen的例子

这很明显,但是如果您只想拉高单元格的行,则可以包裹一个tr循环:)

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

您可以将jQuery与以下代码结合使用


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

希望能帮助到你

暂无
暂无

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

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