2个表,需要为每个匹配的行设置具有最大高度的行(2 tables, need to set the rows with the maximum height for each matching row

编程入门 行业动态 更新时间:2024-10-25 04:16:46
2个表,需要为每个匹配的行设置具有最大高度的行(2 tables, need to set the rows with the maximum height for each matching row)

我有2个html表,我需要每行具有相同的高度。

因此,如果表#1的第3行的高度为25,那么表#2的第3行的高度应为25。

无论哪个匹配行具有最大高度,则两个行应具有相同的高度。

我怎样才能做到这一点?

我知道如何遍历行如:

$("table1 tr").each(function() { }); $("table2 tr").each(function() { });

I have 2 html tables, I need to have each row with the same height.

So if table#1's 3rd row has a height of 25, then table#2's 3rd row should have a height of 25.

Whichever matching row has the largest height, then both rows should have the same height.

How can I do this?

I know how to traverse the rows like:

$("table1 tr").each(function() { }); $("table2 tr").each(function() { });

最满意答案

你可以这样做(假设两个表有相同的行数):

//for each row in table one $("#table1 tr").each(function(index, element){ //get row height from table1 and the corresponding row in table two var rowOneHeight = $(this).height(); var rowTwo = $("#table2 tr:eq(" + index + ")"); //if no matching row was found in table two, stop loop if(!rowTwo.length) return false; var rowTwoHeight = rowTwo.height(); //compare row heights, and set the least high row to the height of the highest one if(rowOneHeight > rowTwoHeight){ //set rowTwoHeight to rowOneHeight rowTwo.height(rowOneHeight); }else{ $(this).height(rowTwoHeight); } });

You can do something like this (assuming both tables have an equal amount of rows):

//for each row in table one $("#table1 tr").each(function(index, element){ //get row height from table1 and the corresponding row in table two var rowOneHeight = $(this).height(); var rowTwo = $("#table2 tr:eq(" + index + ")"); //if no matching row was found in table two, stop loop if(!rowTwo.length) return false; var rowTwoHeight = rowTwo.height(); //compare row heights, and set the least high row to the height of the highest one if(rowOneHeight > rowTwoHeight){ //set rowTwoHeight to rowOneHeight rowTwo.height(rowOneHeight); }else{ $(this).height(rowTwoHeight); } });

更多推荐

本文发布于:2023-07-09 02:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1082872.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:高度   set   tables   rows   row

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!