gpt4 book ai didi

javascript - 将一个表格列表中的单元格高度分配给另一个列表

转载 作者:行者123 更新时间:2023-11-27 23:49:16 24 4
gpt4 key购买 nike

有几对<table> s 在 html 页面上。

每对表的行数相同。

每对的第一个表都有类 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 或其他 classes 可能会被分配(但出于某种原因我不想这样做)。

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

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

谢谢。

最佳答案

你可以这样做:

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

关于javascript - 将一个表格列表中的单元格高度分配给另一个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28157982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com