Javascript:使用外部脚本

时间:2012-08-13 03:35:21

标签: javascript

我正在尝试将一个简单的脚本导入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>

2 个答案:

答案 0 :(得分:0)

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

document.attachEvent(document.body, "load", myFunction)

答案 1 :(得分:0)

<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>
相关问题