gpt4 book ai didi

javascript - 单击 2 次显示文本 javascript

转载 作者:行者123 更新时间:2023-12-03 02:42:40 25 4
gpt4 key购买 nike

我现在正在用 javascript 编写一个小点击游戏,目前遇到了一个小挑战。我需要它,以便每当我点击按钮 10 次时。我的第二个值应该增加一。可能有点难以理解,我会尝试用代码来解释。

// Let's just say I have this variable.
var timesThatTheButtonHasBeenClickedTenTimes = 0;
// So let's say I have an amount of times clicked.
Amount = 0;

// Whenever I click the button..The Amount increases like this.
Amount++;

// so after one click the amount should be..
Amount = 1;
// I need it so that when the button has been clicked 10 times I want to display //that. Something like this.

timesThatTheButtonHasBeenClickedTenTimes = 1;

我应该用 while 循环还是什么来做到这一点?

最佳答案

// Let's just say I have this variable.
var timesThatTheButtonHasBeenClickedTenTimes = 0;
// So let's say I have an amount of times clicked.
var amount = 0;

var counter = function () {
amount++;

if (amount === 10) {
amount = 0;
timesThatTheButtonHasBeenClickedTenTimes++;
}

document.getElementById('clicks').innerHTML = amount;
document.getElementById('hits').innerHTML = timesThatTheButtonHasBeenClickedTenTimes;
};

document.getElementById("mybutton").addEventListener("click", counter);
<button id='mybutton'>
Click me!
</button>
<p>
Clicks = <span id='clicks'>0</span>
</p>

<p>
10 times hits = <span id='hits'>0</span>
</p>

希望有帮助!

关于javascript - 单击 2 次显示文本 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48254878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com