3
<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">&nbsp;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 新手。

4

3 回答 3

5

speed最后一行中的变量与您设置的变量不同,因为它位于不同的范围内。

试试这个:

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;
  }
};
于 2013-02-18T06:15:52.610 回答
2

原因是范围:

var calculate = function() {
  var distance = document.getElementById("distance").value;
  var time = document.getElementById("time").value;
  var speed = distance/time;
}

// speed is not within the right scope anymore 
// because you closed the function above!
if (speed=="Infinity" || speed=="NaN"){
  document.getElementById("speed").innerHTML="Invalid Input";
  document.getElementById("unit").innerHTML="";
}else{
  document.getElementById("speed").innerHTML=speed;
}

因为您的脚本在其中运行document,所以 的值speed被解析为元素<span id="speed" ... >

移动函数内的最后一个 if 语句:

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;
  }
}
于 2013-02-18T06:17:33.123 回答
0

这是一个不明显的问题。它有助于适当的缩进

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;
}

您分配的值speed实际上并未在代码中的任何位置定义。不使用speedfrom inside ofcalculate是因为它在函数范围内。您实际分配的speed是浏览器根据id您提供给元素的内容自动创建的内容。

如果您将作业放在您的calculate中,问题将得到解决。

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;
  }
}
于 2013-02-18T06:17:39.983 回答