这是我第一次从一些警报和第一篇文章中写过的javascript。所以我创建了一个Html页面和一个外部javascript文件。两个文件都在同一个目录中,我检查了每个文件的拼写。
这是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文件:
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代码有效吗?
答案 0 :(得分:6)
您忘记了函数名称末尾的()
。
<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>