Javascript 外部文件未正确链接
问题描述:
这是我写的第一个 javascript,除了一些警报和我的第一篇文章.所以我创建了一个 Html 页面和一个外部 javascript 文件.两个文件都在同一个目录中,我已经检查了每个文件的拼写.
This is my first javascript that I have ever wrote aside from a few alerts and also my first post. So I have created an Html page and a external javascript file. Both files are in the same directory and I have checked the spellings of each file.
这是html代码:
<body>
<p>Press the buttons to change the box!</p>
<div id="box" style="height:150px; width:150px; background-color:orange; margin:25px"></div>
<button id="button1" onclick="divGrow">Grow</button>
<button id="button2" onclick="divColor">Blue</button>
<button id="button3" onclick="divFade">Fade</button>
<button id="button4" onclick="divReset">Reset</button>
<script type="text/javascript" src="javascript.js"></script>
</body>
这里是javascript文件:
and here is the javascript file:
function divGrow() {
alert("This grows the box");
document.getElementById("box").style.width = "300px";
document.getElementById("box").style.height = "300px";
}
function divColor() {
alert("This changes the Box to Blue");
document.getElementById("box").style.backgroundColor = "Blue";
}
function divFade() {
alert("This fades the box");
document.getElementById("box").style.opacity = ".5";
}
function divReset() {
alert("This resets the box")
document.getElementById("box").style.backgroundColor = "Orange"
document.getElementById("box").style.width = "150px";
document.getElementById("box").style.height = "150px";
document.getElementById("box").style.opacity = "0";
}
我错过了另一个链接吗?使 JavaScript 代码正常工作?
Am I missing another link? To make the javascript code work?
答
您忘记了函数名称末尾的 ()
.
You forgot the ()
at the end of the function names.
<button id="button1" onclick="divGrow()">Grow</button>
<button id="button2" onclick="divColor()">Blue</button>
<button id="button3" onclick="divFade()">Fade</button>
<button id="button4" onclick="divReset()">Reset</button>