为什么我的JavaScript文件无法加载? [英] Why my javascript file doesn't load?
本文介绍了为什么我的JavaScript文件无法加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的html文件的代码:
This is the code of my html file:
<html>
<head>
<script type="text/javascript" src="try1.js"></script>
</head>
<body>
<h1>A Web Page</h1>
<p id="demo">A Paragraph</p>
<button type="button" onclick="myFunction()">Try it</button>
</body>
</html>
这是我的javascript文件的代码:
And this is the code of my javascript file:
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed";
}
推荐答案
因为该元素尚不存在.尝试将javascript放在正文的最后
Because the element doesn't exist yet. Try putting the javascript at the end of the body
<html>
<head>
<title>Example<title>
</head>
<body>
<h1>A Web Page</h1>
<p id="demo">A Paragraph</p>
<button type="button" onclick="myFunction()">Try it</button>
<script type="text/javascript" src="try1.js"></script>
</body>
</html>
内联时似乎很好用.
<h1>A Web Page</h1>
<p id="demo">A Paragraph</p>
<button type="button" onclick="myFunction()">Try it</button>
<script type="text/javascript">
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed";
}
</script>
这篇关于为什么我的JavaScript文件无法加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文