2012-08-13 72 views
0

我想将一个简单的脚本导入到HTML文档中。它不会工作,我不知道我做错了什么。Javascript:使用外部脚本

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

window.onload = myFunction; 

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

名称 - >(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> 
+0

似乎没有问题的代码,你得到任何错误? – xdazz 2012-08-13 03:38:49

+0

你可以分享文件夹结构吗? – 2012-08-13 03:40:06

+1

“function.js”文件是否与'attributes.html'位于同一目录级别? – 2012-08-13 03:41:12

回答

0

我觉得身体上的负荷事件在js文件运行之前发生得太早。 尽量使用“document.body.onload”至少“window.onload”。 尝试使用“document.attachEvent”:

document.attachEvent(document.body, "load", myFunction) 
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>