[英]Fade And Reset Buttons Not Responding On Javascript & HTML files
I'd like to disclose that I am extremely new to all of this and have minor understanding of terms etc. I have prework assigned for my course and it's instructing me to "use JavaScript to change the CSS properties of a box upon button clicks".我想透露,我对这一切都非常陌生,并且对术语等有一点了解。我为我的课程分配了预习,它指示我“使用 JavaScript 在按钮单击时更改框的 CSS 属性” .
I've managed to make the "grow" and "blue" buttons work, but the other two are unresponsive.我设法使“增长”和“蓝色”按钮工作,但其他两个没有反应。 I'm using VS Code.我正在使用 VS 代码。 I'd appreciate any simple explanations to any problems in my code.对于我的代码中的任何问题,我将不胜感激。 Thanks!谢谢!
document.getElementById("growBtn").addEventListener("click", function(){document.getElementById("box").style.height = "250px"}); document.getElementById("blueBtn").addEventListener("click", function(){document.getElementById("box").style.backgroundColor = "blue" }); document.getElementById("fadeBtn").addEventListener("click", function(){document,getElementById("box").style.backgroundColor = "lightOrange" }); document.addEventListener("resetBtn").addEventListener("click", function(){document.getElementById("box").style.height = "150px"});
<: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="growBtn">Grow</button> <button id="blueBtn">Blue</button> <button id="fadeBtn">Fade</button> <button id="resetBtn">Reset</button> <script type="text/javascript" src="javascript.js"></script> </body> </html>
Three problems:三个问题:
document,getElementById
has a comma instead of a dot.一个错字: document,getElementById
有一个逗号而不是一个点。addEventListener
in the place of getElementById
.您的第四个 function 有一个明显的复制粘贴错误,用addEventListener
代替getElementById
。Corrected version:修正版:
document.getElementById("growBtn").addEventListener("click", function() { document.getElementById("box").style.height = "250px" }); document.getElementById("blueBtn").addEventListener("click", function() { document.getElementById("box").style.backgroundColor = "blue" }); document.getElementById("fadeBtn").addEventListener("click", function() { document.getElementById("box").style.backgroundColor = "peachpuff" }); document.getElementById("resetBtn").addEventListener("click", function() { document.getElementById("box").style.height = "150px" });
<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="growBtn">Grow</button> <button id="blueBtn">Blue</button> <button id="fadeBtn">Fade</button> <button id="resetBtn">Reset</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.