第一次发帖,因为我犯的任何错误而提前道歉。 当我在Chrome / Firefox中运行此代码时,我会收到一个初始警告框,要求输入一个数字。我输入一个数字,没有任何反应。这之前有用,但是当我回来检查代码中我遇到的另一个类似问题时,它不会打开第二个警告框。
Task2.html文件
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src ="task2.js"></script>
</head>
<body>
<script>
var tempSelect = 0;
while (tempSelect != 3) {
tempSelect = prompt("1.Fahrenheit to Celcius\n2.Celcius to Fahrenheit\n3.To Exit");
if (tempSelect == 1) {
Cel();
}
else if (tempSelect == 2) {
Fahr();
}
}
</script>
</body>
</html>
Task2.js文件
function Cel() {
check = true;
var fahr = prompt("Enter the degree in Fahrenheit to convert to Celcius");
parseFloat(fahr);
while (check == isNaN(fahr)) <!--isNaN = is Not a number--><!--looping till a valid number is entered-->
{
alert("Enter a correct number!");
fahr = prompt("Enter the degree in Fahrenheit to convert to Celcius");
}
var cel = ((5.0 / 9.0) * (fahr - 32)); <!--calculations -->
parseFloat(cel);
alert("Fahr to Cel ===> " + cel); <!--Output-->
}
function Fahr() {
check = true;
var cel = prompt("Enter the degree in Celcius to convert to Fahrenheit");
parseFloat(fahr);
while (check == isNaN(cel)) {
alert("Enter a correct number!");
var cel = prompt("Enter the degree in Celcius to convert to Fahrenheit");
}
var fahr = (9.0 / 5.0 * cel) + 32;
alert("Cel to Fahr ===> " + fahr);
}
答案 0 :(得分:4)
啊!我们都忽略了显而易见的事实:你的js文件中有HTML注释,导致javascript失败。
在javascript中使用/* comment */
或// comment
代替<!-- comment -->
。