请教如何用jquery求和数值和生成html
1:表格data1是手动添加2:如果存在data1,就根据data1生成两个表格data2和data3到div2里面去,data2和data3里面的数值为data1的求和...
1:表格data1是手动添加
2:如果存在data1,就根据data1生成两个表格data2和data3到div2里面去,data2和data3里面的数值为data1的求和 展开
2:如果存在data1,就根据data1生成两个表格data2和data3到div2里面去,data2和data3里面的数值为data1的求和 展开
2个回答
展开全部
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="Scripts/jquery-1.8.2.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
if ($("#data1").length > 0) {
var tableHtml = "";
//data2
tableHtml += "<table id='data2'>";
tableHtml += "<tr>" + $("#data1 tr:eq(0)").html() + "</tr>";
tableHtml += "<tr><td>合计</td>";
$("#data1 tr:eq(0) th:gt(0)").each(function () {
var total = 0;
$("#data1 tr:gt(0) td:nth-child(" + ($(this).index() + 1) + ")").each(function () {
total += parseInt($(this).html());
});
tableHtml += "<td>" + total + "</td>";
});
tableHtml += "</tr></table>";
//data3
tableHtml += "<table id='data3'>";
tableHtml += "<tr><td></td><td>合计</td></tr>";
$("#data1 tr:gt(0)").each(function () {
var total = 0;
$(this).children("td").each(function (i) {
if (i == 0) {
tableHtml += "<tr><td>" + $(this).html() + "</td><td>";
}
else {
total += parseInt($(this).html());
}
});
tableHtml += total + "</td></tr>";
});
tableHtml += "</table>";
$("div.2").append(tableHtml);
}
});
</script>
</head>
<body>
<div class="div1">
<table id="data1">
<tr>
<th></th>
<th>服装</th>
<th>包包</th>
<th>鞋子</th>
</tr>
<tr>
<td>8:00</td>
<td>3</td>
<td>4</td>
<td>1</td>
</tr>
<tr>
<td>9:00</td>
<td>4</td>
<td>2</td>
<td>7</td>
</tr>
<tr>
<td>10:00</td>
<td>2</td>
<td>7</td>
<td>3</td>
</tr>
</table>
</div>
<div class="2">
</div>
</body>
</html>
代码应该还可以再优化。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询