繁体   English   中英

Javascript:使用外部脚本

[英]Javascript: Using External Scripts

我正在尝试将简单的脚本导入HTML文档。 它不会工作,我不知道我做错了。

外部脚本的名称->(function.js)

window.onload = myFunction;

       function myFunction() 
       {
             document.getElementById("HERE").innerHTML = "message";
       }

HTML doc的名称->(attributes.html)

<!DOCTYPEhtml>

    <html>
    <head>

         <title> This is an example of using an external script</title> 
            <script type ="text/javascript" src= "function.js"></script>
    </head>
    <body>

          <h1 id="HERE"> 
          </h1>

    </body>
    </html>

我认为加载事件的主体在js文件运行之前触发得太早。 尝试至少在“ window.onload”中使用“ document.body.onload”。 尝试使用“ document.attachEvent”:

document.attachEvent(document.body, "load", myFunction)
<html>
<head>

     <title> This is an example of using an external script</title> 
        <script type ="text/javascript" src= "function.js"></script>
</head>
<body>

      <h1 id="HERE"> 
      </h1>

     <script>myFunction() </script>
</body>
</html>

暂无
暂无

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

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