我是编程新手,正在创建一个简单的JavaScript应用程序,用于转换输入的Celsius值并输出Fahrenheit。它似乎不起作用,我做错了什么。
function CelciusToFarenheit() {
a = document.getElementById("Celcius").value;
b = document.getElementById("Farenheit");
f = (a * (9/5) + 32);
b.innerHTML = f;
}

<html>
<head>
<title>Projects</title>
<script src="pr.js" type="text/js"></script>
<link href="main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="div1">
Convert celcius to farenheit:
<br>
<br>
Celcius: <input type="number" name="Celcius" id="Celcius"><br>
Farenheit: <span id="Farenheit"></span><br>
<button id="submit" onclick="CelciusToFarenheit()">Calculate</button>
</div>
</body>
</html>
&#13;
答案 0 :(得分:0)
您想要更改名称或将id=Celcius
添加到输入
function CelciusToFarenheit() {
a = document.getElementById("Celcius").value;
b = document.getElementById("Farenheit");
f = (a * (9 / 5) + 32);
b.innerHTML = f;
}
<html>
<head>
<title>Projects</title>
<script src="pr.js" type="text/js"></script>
<link href="main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="div1">
Convert celcius to farenheit:
<br>
<br> Celcius: <input type="number" id="Celcius"><br> Farenheit: <span id="Farenheit"></span><br>
<button id="submit" onclick="CelciusToFarenheit()">Calculate</button>
</div>
</body>
</html>
答案 1 :(得分:0)
您尚未将 Celcius 作为 ID ,而是将名称放在HTML代码中。
这样做:
<html>
<head>
<title>Projects</title>
<script src="pr.js" type="text/js"></script>
<link href="main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="div1">
Convert celcius to farenheit:
<br>
<br>
Celcius: <input type="number" id="Celcius" name="Celcius"><br>
Farenheit: <span id="Farenheit"></span><br>
<button id="submit" onclick="CelciusToFarenheit()">Calculate</button>
</div>
</body>
</html>
我建议您在某些代码之前和之后使用<p>
或div
而不是<br>
之类的标记。就像在这种情况下一样。
<br>Celcius: <input type="number" id="Celcius" name="Celcius"><br>
答案 2 :(得分:0)
您在输入字段中仅声明了name not id。写id
<html>
<head>
<title>Projects</title>
<script src="pr.js" type="text/js"></script>
<link href="main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="div1">
Convert celcius to farenheit:
<br>
<br>
Celcius: <input type="number" name="Celcius" id="Celcius"><br>
Farenheit: <span id="Farenheit"></span><br>
<button id="submit" onclick="CelciusToFarenheit()">Calculate</button>
</div>
</body>
</html>
JS:
function CelciusToFarenheit() {
a = document.getElementById("Celcius").value;
b = document.getElementById("Farenheit");
f = (a * (9 / 5) + 32);
b.innerHTML = f;
}
你的脚本不变?