微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 在jQuery中将一列的内容复制到另一列

以下jQuery非常慢(约7秒).我显然做错了!

我正在尝试将列col的内容复制到HTML表中的第0列
所以如果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:

最佳答案
您无需单独选择每个表格单元格.您可以选择源列和目标列并迭代它们:

// 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());
});

原文地址:https://www.jb51.cc/jquery/428014.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐