[英]My clicker game won't work
I started to work on a clicker game in JavaScript, but it's not working.我开始用 JavaScript 开发一个点击游戏,但它不起作用。 I used the console to inspect the element to find out why it won't add anything to
money
and I don't think it is showing the variable either because it won't even show a 0.我使用控制台检查元素以找出为什么它不会向
money
添加任何东西,我认为它也不会显示变量,因为它甚至不会显示 0。
<!doctype html> <html lang="en"> <head> <script> function addMoney() { var money money+1; var cash = document.getElementById("showmoney"); cash.innerHTML=money; } </script> <meta charset="utf-8"> <title>clicker game</title> </head> <p id="showmoney"></p> <body> <button id="click" onClick="addMoney">click</button> </body> </html>
The call to the function should have parenthesis ()
:对函数的调用应该有括号
()
:
<button id="click" onClick="addMoney()">
Instead of :代替 :
<button id="click" onClick="addMoney">
Also you have to init you variable money
and to define it in the global scope so it will not return to default value zero on every click :此外,您必须初始化变量
money
并在全局范围内定义它,这样每次单击时它都不会返回默认值零:
var money=0;
Hope this helps.希望这可以帮助。
<!doctype html> <html lang="en"> <head> <script> var money=0; function addMoney(){ money++; var cash = document.getElementById("showmoney"); cash.innerHTML=money; } </script> <meta charset="utf-8"> <title>clicker game</title> </head> <p id="showmoney"> </p> <body> <button id="click" onClick="addMoney()"> click </button> </body> </html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.