为什么我看不到输出?

时间:2015-10-14 18:36:14

标签: javascript html5

我一直在学习javascript的基础知识。我写了一些代码来找到圆柱体的体积。我的代码在W3C上验证但是我看不到输出。你们能指导我正确的方向吗?谢谢!

<!DOCTYPE HTML>
<html lang="en-us">
<head>
    <meta charset="utf-8">
    <title> Variables and Arithmatic Example 1</title>

        <script type="text/javascript">
        /*Input: Radius and  height of a cylinder.
         *Processing: Compute volume of cylinder.
         *Output: Volume of cylinder.
        */
        function volume() {
        // Get the radius and height from user.
            var r = parseInt(document.getElementById("radiusInputBox").value);
            var h = parseInt(document.getElementById("heightInputBox").value);

        //Compute volume of cylinder.
            var v = Math.PI * r * r * h;

        //Display volume to user.
        document.getElementByID("volumeDiv").innerHTML = v;
        }               
        </script>
</head>
<body>
<h1>Cylinder Volume Calculator</h1>
<h2>Please enter value for cylinder radius and height:</h2>
    Radius <input type="text" id="radiusInputBox" size="3"><br>
    Height <input type="text" id="heightInputBox" size="3"><br>
<button type="button" onclick="volume()">Volume</button>
<div id="volumeDiv"></div>
</body>

2 个答案:

答案 0 :(得分:1)

它是document.getElementById,而不是document.getElementByID

/*Input: Radius and  height of a cylinder.
 *Processing: Compute volume of cylinder.
 *Output: Volume of cylinder.
 */
function volume() {
  // Get the radius and height from user.
  var r = parseInt(document.getElementById("radiusInputBox").value);
  var h = parseInt(document.getElementById("heightInputBox").value);

  //Compute volume of cylinder.
  var v = Math.PI * r * r * h;

  //Display volume to user.
  document.getElementById("volumeDiv").innerHTML = v;
}
 <h1>Cylinder Volume Calculator</h1>
<h2>Please enter value for cylinder radius and height:</h2>
Radius
<input type="text" id="radiusInputBox" size="3">
<br>Height
<input type="text" id="heightInputBox" size="3">
<br>
<button type="button" onclick="volume()">Volume</button>
<div id="volumeDiv"></div>

答案 1 :(得分:1)

你有document.getElementByID,它需要是document.getElementById。