ppr*_*yer 0 html javascript jquery
我正在努力将javascript功能添加到具有多个按钮的基本HTML页面.当我在外部JS文件中包含一个函数时,代码可以工作,但当我尝试添加另一个按钮功能时,代码停止工作.
我该如何解决这个问题?
HTML:
<!DOCTYPE html>
<html>
<head>
<title>Jiggle Into JavaScript</title>
<!-- <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.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>
<script type="text/javascript" src="javascript.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
片段:
document.getElementById("button1").addEventListener("click", function(){
document.getElementById("box").style.height = "500px";
});
document.getElementById("button2").addEventListener("click", function(){
document.getElementById("box").style.background-color = "blue";
});Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
您的语法不正确:
document.getElementById("box").style.background-color = "blue";
Run Code Online (Sandbox Code Playgroud)
-是减法运算符,它不能用作属性文字的一部分.通过将CSS名称转换为camelCase来创建样式属性,因此它应该是:
document.getElementById("box").style.backgroundColor = "blue";
Run Code Online (Sandbox Code Playgroud)