我对JavaScript很陌生,我无法弄清楚如何使这段代码工作。我有一个HTML页面和一个Javascript页面。我试图让按钮正常工作,但我甚至无法使颜色更改工作。我在哪里错了?我没有正确链接文件吗?初次使用Javascript,颜色变化
HTML:
<!DOCTYPE html>
<html>
<head>
<title>JavaScript practice</title>
<!-- <script type="text/javascript"
src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js">
</script> -->
<script type="text/javascript" src="javascript.js"></script>
</head>
<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">Grow</button>
<button id="button2">Blue</button>
<button id="button3">Fade</button>
<button id="button4">Reset</button>
</body>
</html>
Javascript:
document.getElementById("button2").addEventListener("click", function(){
document.getElementById("box").style.backgroundColor = "blue";
})
是您的JavaScript文件在同一目录作为HTML文件添加类onClick事件? –