2015-01-26 68 views
2

在html頁面上有幾對<table> s。將單元格的高度從一個表格列表分配到另一個列表

每一對錶格都有相同的行數。

每一對的第一個表有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 S或附加類新可分配(但是我不想被一些做原因)。

有什麼建議嗎? P.S.此外,我相信如果我$('table.class1 tr').each()class1而不是class2,表中的行的順序將是相同的(在控制檯中檢查)。

如果任何表格中單元格的高度是可變的,那麼可以。唯一的要求是每一對第一排的高度相等,第二排的高度相等,等等。

謝謝。

+0

您可以使用conceder陣列存儲class1'的'行高度以'each'再有另一個'each'環路將這些存儲高度設置爲'class2'中的行 – 2015-01-26 20:44:00

回答

2

你可以做這樣的事情:

$('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>

相關問題