[英]How to link an external JS file correctly?
<!DOCTYPE html>
<html>
<head>
<title>Post Lecture Task</title>
</head>
<body>
<h1 id="demo" onmouseover="mouseOver">Mouse over me</h1>
<script src="Documents/postlecture.js"></script>
</body>
</html>
如果我在内部包含它,则 JS 可以工作,但我很困惑为什么它在作为外部 js 文件连接时无法工作。
function mouseOver() {
document.getElementById("demo").style.color = "red";
}
js src 它将取决于您的文件所在的位置。 另外,在这个例子中。我认为你应该补充
onmouseover="mouseOver"
使用 onmouseover="mouseOver()"
如果您的 index.html 文件与 postlecture.js 文件位于同一位置,那么您可以执行以下操作,
<!DOCTYPE html>
<html>
<head>
<title>Post Lecture Task</title>
</head>
<body>
<h1 id="demo" onmouseover="mouseOver()">Mouse over me</h1>
<script type="text/javascript" src="postlecture.js"></script>
</body>
</html>
<!--postlecture.js file -->
function mouseOver() {
document.getElementById("demo").style.color = "red";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.