计算结果未显示

时间:2015-05-06 16:49:04

标签: javascript html math xhtml calculator

该程序应该计算屏幕的像素密度,但是当我点击计算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>

1 个答案:

答案 0 :(得分:0)

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

&#13;
&#13;
<!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>
&#13;
&#13;
&#13;