使用parseFloat应用于特定的td类
下面的代码有问题吗?只是试图为特定类别的 td 修复十进制步数。只需要使用 jquery
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<td> 25.252525
</td>
<script type="text/javascript">
$('.go').each(function() {
return parseFloat($(this).toFixed(2));
}
</script>
</body>
</html>
回答
您需要使用.text()从 td 获取文本,然后使用.toFixed()并最终更改tdie 中的更新值:$(this).text(parseFloat($(this).text()).toFixed(2));
演示代码:
$('.go').each(function() {
$(this).text(parseFloat($(this).text()).toFixed(2)); //change text..
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<table>
<tr>
<td> 25.252525
</td>
</tr>
<tr>
<td> 25.2223525
</td>
</tr>
</table>