Jquery 循环得到列td的合计值
<table><tr><td>A</td><td>B</td><td>C</td></tr><tr><td>1</td><td>2</td><td>3</td></tr>...
<table>
<tr>
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
.....
</table>
要得到 1+4+.... , 2+5+...., 3+6....., 展开
<tr>
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
.....
</table>
要得到 1+4+.... , 2+5+...., 3+6....., 展开
2个回答
2015-01-12
展开全部
<!DOCTYPE HTML>
<html>
<head>
<meta charset=UTF-8>
<meta name="keywords" content="白菜编辑部">
<title>白菜编辑部</title>
<style type="text/css">
</style>
<script type="text/javascript" src="jquery-1.8.0.min.js"></script>
<script type="text/javascript">
jQuery (function ($)
{
$ ('table').append ("<tr></tr>");
var len = $ ('table tr:eq(0) td').length;
for ( var i = 0; i < len; i++)
{
var sum = 0;
$ ('table tr:gt(0) td:nth-child(' + (i + 1) + ')').each (function (j, dom)
{
sum += parseFloat ($ (this).text ());
});
$ ('table tr:last').append ("<td>" + sum + "</td>");
}
});
</script>
</head>
<body>
<table>
<tr>
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
</table>
</body>
</html>
展开全部
<script type="text/javascript">
$(function () {
var h1 = 0; //第一列的和
var h2 = 0; //第二列的和
var h3 = 0; //第三列的和
var h = 0; //
$("#table1 tr").each(function () {
if (h == 0) {
h++;
}
else {
h1 += parseInt($(this).find("td:nth-child(1)").html());
h2 += parseInt($(this).find("td:nth-child(2)").html());
h3 += parseInt($(this).find("td:nth-child(3)").html());
}
});
alert(h1);
alert(h2);
alert(h3);
});
</script>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询