gpt4 book ai didi

javascript - jQuery : multi lines operations on an html table

转载 作者:行者123 更新时间:2023-11-28 19:54:18 25 4
gpt4 key购买 nike

我有一个像这样的 html 表格:

time | time_diff
12:01|
12:21|
12:31|

我如何使用Javascript(有或没有jQuery)计算time_diff,这样我的表格看起来像:

time | time_diff
12:01| 20
12:21| 10
12:31|

我基本上想知道如何在 html 表格上实现多行操作?

最佳答案

也许是这样的:

$('table tbody tr').each(function () {

var $row = $(this),
$nextRow = $row.next();

if ($nextRow.length) {
this.cells[1].innerHTML = timeDiff($nextRow.find('td').text(), $row.find('td').text());
}
});

function timeDiff(t1, t2) {
t1 = t1.split(':');
t2 = t2.split(':');
var diff = (t1[0] * 60 + +t1[1]) - (t2[0] * 60 + +t2[1]),
hours = Math.floor(diff / 60),
minutes = (diff - hours * 60) + '';
return hours + ':' + (minutes.length == 1 ? '0' + minutes : minutes);
}

演示:http://jsfiddle.net/vJNa4/1/

关于javascript - jQuery : multi lines operations on an html table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22873080/

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