gpt4 book ai didi

javascript - 使用 colspan 搜索行中单元格数最多的行

转载 作者:行者123 更新时间:2023-11-28 20:50:45 29 4
gpt4 key购买 nike

在具有大量 colspan 的大表中搜索最大数量的单元格的有效方法是什么(合并单元格 ** colspan 应被忽略,因此在下面的示例中,第一行的最大单元格数量为 4)。是带有 reg 表达式的 js/jquery 还是只是带有冒泡排序的循环。我得到了一个链接,如下所示,解释了正则表达式的使用是一种理想的方式...有人可以为此建议伪代码吗?

High cpu consumption due to a jquery regex patch

<table width="156" height="84" border="0" >
<tbody>
<tr style="height:10px">
<td width="10" style="width:10px; height:10px"/>
<td width="10" style="width:10px; height:10px"/>
<td width="10" style="width:10px; height:10px"/>
<td width="10" style="width:10px; height:10px"/>
</tr>
<tr style="height:10px">
<td width="10" style="width:10px; height:10px" colspan="2"/>
<td width="10" style="width:10px; height:10px"/>
<td width="10" style="width:10px; height:10px"/>
</tr>
<tr style="height:10px">
<td width="10" style="width:10px; height:10px"/>
<td width="10" style="width:10px; height:10px"/>
<td width="10" style="width:10px; height:10px" colspan="2"/>
</tr>
<tr style="height:10px">
<td width="10" style="width:10px; height:10px" colspan="2"/>
<td width="10" style="width:10px; height:10px"/>
<td width="10" style="width:10px; height:10px"/>
</tr>
</tbody>
</table>

最佳答案

var rowWithMaxCells = $("tr").get().reduce(function(a, b) {
return a.children.length > b.children.length ? a : b;
});

console.log(rowWithMaxCells);

Example

编辑

function getRowWithMaxCells(table) {
var rows = table.getElementsByTagName("tr"),
i = rows.length,
maxIdx = -Infinity,
curMax = -Infinity;

while (i--) {
if (curMax <= rows[i].children.length) {
curMax = rows[i].children.length;
maxIdx = i;
}
}

return rows[maxIdx];
}

只是为了完整起见,该版本应该适用于所有浏览器:)
Example

关于javascript - 使用 colspan 搜索行中单元格数最多的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12484681/

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