<form>
<table>
<tr>
<td>Distance:</td>
<td><input type="number" id="distance" onKeyUp="calculate();">m</td>
</tr>
<tr>
<td>Time:</td>
<td><input type="number" id="time" onKeyUp="calculate();">s</td>
</tr>
<tr>
<td>Speed:</td>
<td><span id="speed">21</span><span id="unit"> m/s</span></td>
</tr>
</table>
</form>
<script type="text/javascript">
var calculate = function() {
var distance = document.getElementById("distance").value;
var time = document.getElementById("time").value;
var speed = distance/time;}
if (speed=="Infinity" || speed=="NaN") {
document.getElementById("speed").innerHTML="Invalid Input";
document.getElementById("unit").innerHTML="";
} else {
document.getElementById("speed").innerHTML=speed;
}
</script>
我正在制作一个简单的速度计算器,但它不起作用。输出来自[object HTMLSpanElement] 任何帮助将不胜感激,因为我是 javascript 新手。