gpt4 book ai didi

javascript - Javascript 得到 NaN 结果

转载 作者:行者123 更新时间:2023-12-03 02:58:45 26 4
gpt4 key购买 nike

JS代码

'use strict';

$(document).ready(function() {

});

function calculatePPI(inputWidth, inputHeight, inputDiagonal) {
document.getElementById("outputPPI").innerHTML=
((Math.sqrt(Math.pow('inputWidth', 2)) + (Math.pow('inputHeight', 2)))/'inputDiagonal');
}

HTML 代码

<body>

<h2>Length Converter</h2>
<p>The following program will convert your centimeters to inches.</p>

<p>
<label>Width (Pixels):</label>
<input id="inputWidth" type="number" placeholder="Width">
<p>
<label>Height (Pixels):</label>
<input id="inputHeight" type="number" placeholder="Height">
</p>

<p>
<label>Diagonal (Pixels):</label>
<input id="inputDiagonal" type="number" placeholder="Diagonal Length">
</p>

<button onclick="calculatePPI(document.getElementById('inputWidth', 'inputHeight', 'inputDiagonal').value)">Calculate</button>
</p>

<p>PPI: <span id="outputPPI"></span></p>

</body>

每当我尝试计算它时,我都会得到 NaN 结果。

For example

有人可以帮我解决这个问题吗?我不确定代码到底哪里出了问题,因为我是 Javascript 编码新手。

最佳答案

您不能像这样使用 getElementById,并且在函数中不要在函数变量周围放置 ''

像这样使用:

'use strict';

$(document).ready(function() {

});

function calculatePPI(inputWidth, inputHeight, inputDiagonal) {

document.getElementById("outputPPI").innerHTML=
((Math.sqrt(Math.pow(inputWidth, 2)) + (Math.pow(inputHeight, 2)))/inputDiagonal);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>

<h2>Length Converter</h2>
<p>The following program will convert your centimeters to inches.</p>

<p>
<label>Width (Pixels):</label>
<input id="inputWidth" type="number" placeholder="Width">
<p><label>Height (Pixels):</label>
<input id="inputHeight" type="number" placeholder="Height"></p>
<p><label>Diagonal (Pixels):</label>
<input id="inputDiagonal" type="number" placeholder="Diagonal Length"></p>

<button onclick="calculatePPI(document.getElementById('inputWidth').value, document.getElementById('inputHeight').value, document.getElementById('inputDiagonal').value)">Calculate</button>
</p>
<p>PPI: <span id="outputPPI"></span></p>
</body>

关于javascript - Javascript 得到 NaN 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47503579/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com