获取td中的数据,保留两位小数重新赋值

<!DOCTYPE html>
<html lang="zh">

	<head>
		<meta charset="UTF-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<meta http-equiv="X-UA-Compatible" content="ie=edge" />
		<title>Document</title>
	</head>

	<body>

		<table>
			<tr>
				<td class="feeFormat">
					95.121001
				</td>
				<td class="feeFormat">
					93.543001
				</td>
				<td class="feeFormat">
					22.45345
				</td>
				<td class="feeFormat">
					54543.545
				</td>
			</tr>
		</table>

		<script src="jquery.min.js"></script>
		<script>
		
			$("table:eq(0) tr").each(function(index, element) {
				var a = $(element).children(); //$(element)代表每行tr,后面的children代表tr下面的td,a即这一行所有td的集合
				for(var i = 0; i < a.length; i++) {
					console.log(a.eq(i).text()); // 取得index为 i的td里面的文本
					var num = parseFloat(a.eq(i).text());
					var newNum = num.toFixed(2);
					a.eq(i).text(newNum);
				}
			});
		</script>
	</body>

</html>

  

posted @ 2017-08-31 17:59  TigerZhang  阅读(932)  评论(0编辑  收藏  举报