Javascript 外部文件功能对我不起作用,之前可以使用

Javascript external file functions won't work for me, were working before

第一次发帖,如有错误敬请见谅。 当我 运行 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);
}

啊!我们都忽略了一个显而易见的事实:您的 js 文件中有 HTML 注释,这导致 javascript 失败。

在 javascript 中使用 /* comment */// comment 而不是 <!-- comment -->