gpt4 book ai didi

javascript - 使用 jQuery 删除表中的空行和列

转载 作者:搜寻专家 更新时间:2023-11-01 05:07:04 27 4
gpt4 key购买 nike

假设我有一个这样的表:

+-----------+
| | | | | | |
|-+-+-+-+-+-|
| |a| |b| | |
|-+-+-+-+-+-|
| | | | | | |
|-+-+-+-+-+-|
| |c| |d| | |
|-+-+-+-+-+-|
| | | | | | |
+-----------+

我想删除所有空的外部行和列。上面的例子将简化为:

+-----+
|a| |b|
|-+-+-|
| | | |
|-+-+-|
|c| |d|
+-----+

我有一些工作代码,但它不是很优雅,更重要的是,速度慢得令人望而却步。我需要一个可以快速删除多达 30 个无关行和列的解决方案。

是否有一种快速且半途而废的方法来做到这一点?

最佳答案

var $theTable = $("table#myTable"),
lookAt = ["tr:first-child", "tr:last-child",
"td:first-child", "td:last-child"];

for (var i=0; i<lookAt.length; i++) {
while ( $.trim($(lookAt[i], $theTable).text()) == "" ) {
$(lookAt[i], $theTable).remove();
}
}

编辑:您可以将其用作内部循环,也许它会快一点:

for (var i=0; i<lookAt.length; i++) {
while ( var $x = $(lookAt[i], $theTable), $.trim($x.text()) == "" ) {
$x.remove();
}
}

关于javascript - 使用 jQuery 删除表中的空行和列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8571286/

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