试图在calc上显示数字,但它说它不是一个函数。我以前使用过数字(variable.toFixed(X)),但这一次它似乎不能在document.getlementbyid中工作。有什么帮助吗?
if (vi === 0){
document.getElementById('result').innerHTML =
Number(reactant.toFixed(13));
} else if (vi >= 1 && tx === 0) {
document.getElementById('result').innerHTML =
Number(reactant2.toFixed(13));
} else if (vi >= 1 && tx > 0) {
document.getElementById('result').innerHTML =
Number(reactantspec.toFixed(13));
}发布于 2018-06-18 22:50:59
请看下面的示例。希望能帮上忙。
<!DOCTYPE html>
<html>
<body>
<p>Click the button to display the fixed number.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var reactant = '5.5678935436453256434';
var n = Number(reactant).toFixed(13);
document.getElementById("demo").innerHTML = n;
}
</script>
</body>
</html>
发布于 2018-06-18 22:48:53
我不知道reactant是从哪里来的,但更有可能的是,它是一个字符串值,而不是数字。
"11.12123".toFixed(2); <-- toFixed is not a function error (becuase "11.12" is a string)
11.12123.toFixed(2); <-- Success!在运行toFixed逻辑之前,可能需要将字符串转换为数字:
Number("11.123").toFixed(2)发布于 2018-06-18 22:52:33
元素id (即document.getElementById('result').innerHTML )的返回值是一个字符串,应该使用parseInt()将其转换为整数。
https://stackoverflow.com/questions/50918392
复制相似问题