本文介绍了为什么我的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文件无法加载?
发布评论