计算结果不显示

The result of a calculation is not displaying

该程序本应计算屏幕的像素密度,但当我点击“计算 PPI”按钮时,似乎什么也没有发生。宽度、高度和屏幕尺寸应该分别是水平分辨率(以像素为单位)、垂直分辨率(以像素为单位)和对角线屏幕尺寸(以英寸为单位)。谁能告诉我我做错了什么以及如何纠正它?

代码如下:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<style> #ppi { border:none; }
body { font-family:yu gothic; }
 </style>

<body>
<p>
  <label for="Width">Width</label>
  <input type="text" name="Width" id="Width">
  <label for="height">Height</label>
  <input type="text" name="height" id="height">
</p>
<p>
  <label for="ScreenSize">Screen Size</label>
  <input type="text" name="ScreenSize" id="ScreenSize">
</p>
<p>
  <input type="button" name="button" id="button" value="Calculate PPI" onClick="calc()">
</p>
<p>
  <label for="PPI">PPI</label>
  <input type="text" name="PPI" id="ppi">
</p>

<script>
function calc() {
var w = parseInt(document.getElementById("Width")),
h = parseInt(document.getElementById("height")),
s = parseFloat("10.00")(document.getElementById("ScreenSize")),
ppi = (w*w) + (h*h);
ppi = Math.sqrt(ppi);
ppi = ppi/s;`enter code here`
document.getElementById("ppi").value = ppi;
}
</script>
</body>
</html>

您正在设置 onClick。相反,它应该是 onclick。此外,您正在计算 "10.00" 的 parseFloat,它只是 10。最后,您正在尝试解析 widthheightelement;您需要获取元素的 .value

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<style> #ppi { border:none; }
body { font-family:yu gothic; }
 </style>

<body>
<p>
  <label for="Width">Width</label>
  <input type="text" name="Width" id="width">
  <label for="height">Height</label>
  <input type="text" name="height" id="height">
</p>
<p>
  <label for="ScreenSize">Screen Size</label>
  <input type="text" name="ScreenSize" id="ScreenSize">
</p>
<p>
  <input type="button" name="button" id="button" value="Calculate PPI" onclick="calc()">
</p>
<p>
  <label for="PPI">PPI</label>
  <input type="text" name="PPI" id="ppi">
</p>

<script>
function calc() {
var w = parseInt(document.getElementById("width").value),
h = parseInt(document.getElementById("height").value),
s = 10*parseInt(document.getElementById("ScreenSize").value);
ppi = (w*w) + (h*h);
ppi = Math.sqrt(ppi);
ppi = ppi/s;
document.getElementById("ppi").value = ppi;
}
</script>
</body>
</html>