下面的 jQuery 非常慢(约 7 秒)。我显然做错了!
我正在尝试复制列的内容col
到专栏0
在 HTML 表格中
所以如果 col 是 2,那么我需要将第 2 列复制到第 0 列。
for (var i=0;i<31;i++)
$('.grid tr:nth-child(' + i + ') td:first-child').text(
$('.grid tr:nth-child(' + i + ') td:nth-child(' + col + ')').text()
);
HTML:
<table>
<tr><td>A</td><td>D</td><td>G</td></tr>
<tr><td>B</td><td>E</td><td>H</td></tr>
<tr><td>C</td><td>F</td><td>I</td></tr>
<!-- etc. -->
</table>
您不需要单独选择每个表格单元格。您可以选择源列和目标列并迭代它们:
// Get the target column table cells. This will select the first cell from
// each row in the table.
var target = $('.grid tr td:first-child');
// Iterate over each cell in the source column and copy its text to the
// corresponding cell in the target column.
$('.grid tr td:nth-child(' + (col + 1) + ')').each(function (rowIndex) {
target.slice(rowIndex, rowIndex + 1).text($(this).text());
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)