javascript 如何使用 jQuery 找到最大的表格单元格高度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4127040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I find the largest table cell height with jQuery?
提问by sova
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?
我有一个包含可变长度文本内容单元格的表格。我想找到最高单元格的高度,然后将所有单元格设为该高度。我怎样才能做到这一点?
回答by Tatu Ulmanen
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.
用简单的英语,遍历所有单元格并找到最大值,然后将该值应用于所有单元格。
回答by John Strickler
Just to be different:
只是为了不同:
var heights = $('td').map(function() {
return $(this).height();
}).get();
var maxHeight = Math.max.apply(Math, heights);
$('td').css('height', maxHeight);
回答by Hedde van der Heide
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);
});
回答by Jason McCreary
There are several pluginsthat 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});
回答by Ehsan
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
希望能帮助到你

