我正在用 JavaScript 和 HTML 制作一个在线游戏(一个非常简单的游戏,这是我的第一个游戏)。它工作正常,但有一个大问题。我希望每当单击特定图像时,都会将 10 添加到变量分数的值中。这是我使用的代码:
var score = 0;
function addScore() {
var test = parseInt(score);
var score = parseInt(test) + 10;
document.getElementById("score").innerHTML=score; }
我想要的图像具有以下功能:
<img src='mole.png' alt='mole' onclick='addScore()' />
有什么问题,我该如何解决?
最佳答案
你可以通过改变这个让你的生活更简单:
1. Don't use the same varibale name in globally & locally.
2. Removing the `parseInt()` method as score is already an Integer.
所以你的最终脚本应该是:
var score = 0;
function addScore() {
score += 10;
document.getElementById("score").innerHTML=score;
}
关于javascript - "Score"变量没有改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16621732/