将单元格的高度从一个表列表指定到另一个列表

assign heights of cells from one list of tables to another list

本文关键字:列表 一个 另一个 单元格 高度      更新时间:2023-09-26

html 页面上有几对<table>

每对表具有相同的行数。

每对的第一个表都有类class1

每对的第二个表具有类class2

问题是:每个单元格在两个表中的高度应该相同。如何使用jQuery制作它?

我正在考虑使用.eacheq或两者的组合,例如:

$('table.class2 tr').each(function(i){
      ????? = $(this).height();//suppose height of cells in class2 is bigger or the same so we shouldn't care about finding max value and assigning to the `tr` with smaller height
});

作为一种选择,可以分配id或其他类(但是由于某种原因我不想这样做)。

有什么建议吗?附言我也相信如果我使用 class1 而不是 class2 $('table.class1 tr').each(),表中行的顺序将是相同的(在控制台中检查)。

如果任何表中单元格的高度是可变的,则可以。唯一的要求是对于每对,第一行的高度是相等的,第二行的高度是相等的,依此类推。

谢谢。

你可以做这样的事情:

$('table.class2 tr').each(function(i){
  var $table1Elem = $('table.class1 tr').eq(i);
  if($table1Elem.height()>$(this).height()){
    $(this).height($table1Elem.height());     
  }else{
    $('table.class1 tr').eq(i).css('height',$(this).height());
  }
});
<div style="float:left; width:49%;">
                <table class="class1">
                    <tr><td>text 1</td></tr>
                    <tr><td>text<br><br>2</td></tr>
                    <tr><td>text 3</td></tr>        
                </table>
            </div>
            <div style="float:left; width:49%;">
                <table class="class2">
                    <tr><td>text 1</td></tr>
                    <tr><td>text 2</td></tr>
                    <tr><td>text <br><br>3</td></tr>        
                </table>
            </div>

相关文章: