[英]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.