繁体   English   中英

Javascript文件无法加载

[英]Javascript file will not load

我有一个简单的脚本,当包含在html正文中时可以工作。 但是,从相对路径运行时,包括

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

在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>

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM