Javascript 文件将无法加载

Javascript file will not load

我有一个简单的脚本,当包含在 html body 中时可以使用。但是,当从相对路径 运行ning 时,并包括

<script type="text/javascript" src="simple.js"></script>

在 header 或 html 文件的底部,脚本将不再 运行。

谁能指出我遗漏了什么?

谢谢。

脚本simple.js:

<script>
        function firstFunction(){

            var a = document.getElementById("firstInput").value;

            if(! a){
                alert("minimum input of 1 char required");
            }else{
                document.getElementById("firstOutput").innerHTML=a;
            }


        }

        function secondFunction(){
            var b = document.getElementById("secondInput").value;

            if(!b){
                alert("minimum input of 1 char required");
            }


            document.getElementById("secondOutput").innerHTML=b;
            //document.getElementById("secondOutput").innerHTML = "updated!";
        }

如果将 javascript 包含到 html 文件中,则只需要 <script> 标签。

在 .js 文件中,这是一个语法错误。只需编写不带标签的 javascript 代码!

文件simple.js:

function firstFunction(){

        var a = document.getElementById("firstInput").value;

        if(!a){
            alert("minimum input of 1 char required");
        }else{
            document.getElementById("firstOutput").innerHTML=a;
        }


    }

    function secondFunction(){
        var b = document.getElementById("secondInput").value;

        if(!b){
            alert("minimum input of 1 char required");
        }


        document.getElementById("secondOutput").innerHTML=b;
        //document.getElementById("secondOutput").innerHTML = "updated!";
    }

由于您的文件是正确的,请确保将脚本放在结束正文标记之前

<script type="text/javascript" src="simple.js"></script>
</body>

以便在脚本为运行时可以找到元素。