[英]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.