如何检查,单独和总共单击一次动态创建按钮的频率?
我创建了一个带有按钮的循环。我还创建了一个EventListener,可以为每个按钮保持计数。我希望在所有按钮都至少按至少3次按下时会发生一些事情,所以我创建了一个IF语句,检查计数是否为3,并将1添加到另一个计数中,但另一个计数仍在1上,即使按下了多个按钮3次。
for (let i = 0; i < 7; i++) {
let btn = document.createElement('button');
btn.classList.add('buttons');
btn.id = 'button' + i;
document.getElementById('box').appendChild(btn);
let count = 0;
let count2 = 0;
btn.addEventListener('click', () => {
count++;
console.log(count);
if (count === 3) {
console.log('3');
count2++;
console.log(count2);
} else {
console.log('not 3');
}
});
}
#box button { min-width: 20px; min-height: 20px; }
.as-console-wrapper { max-height: 87%!important; }
body { margin: 0; }
<div class="box" id="box">
</div>
因此,计数变量可以正常工作,当单击1个按钮3倍时,计数2变量更改为1,但它停留在1上,我似乎找不到原因。最后,Count2应该是7,因为有7个按钮。
I created a loop with buttons in it. I also created an eventListener that keeps a count for every button. I want something to happen when all buttons have been presses at least 3 times, so I created an if statement that checks if the count is 3 and adds 1 to another count, but the other count stays on 1 even if multiple buttons have been pressed 3 times.
for (let i = 0; i < 7; i++) {
let btn = document.createElement('button');
btn.classList.add('buttons');
btn.id = 'button' + i;
document.getElementById('box').appendChild(btn);
let count = 0;
let count2 = 0;
btn.addEventListener('click', () => {
count++;
console.log(count);
if (count === 3) {
console.log('3');
count2++;
console.log(count2);
} else {
console.log('not 3');
}
});
}
#box button { min-width: 20px; min-height: 20px; }
.as-console-wrapper { max-height: 87%!important; }
body { margin: 0; }
<div class="box" id="box">
</div>
So the count variable works fine, and when 1 button has been clicked 3 times the count2 variable changes to 1, but it stays on 1 and I can't seem to find out why. At the end count2 should be 7, because there are 7 buttons.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了所有已经指出OP问题的评论外,我还建议基于存储/更新a
map
-instance。引入 event-delegation 顶部将消除多个(对于每个动态创建的按钮) )应用的事件处理程序迫使处理不同的计数器示波器的方法(处理程序范围和块范围的
)。
单个处理程序将完全订阅一次单个父母节点的
'单击'
-event。该处理程序通过查找单个点击计数来跟踪并处理各种点击计数状态。In addition to all the comments which already did point to the OP's problem I suggest a different approach based on storing/updating individual button-counts within a
Map
-instance.With introducing event-delegation on top one would get rid of multiple on the fly (for each dynamically created button) applied event handlers which forces the approach to deal with different counter scopes (handler scope and the
for
block scope).There will be exactly a single handler subscribed once to a single parent-node's
'click'
-event. This handler keeps track of and handles the various click-count states by looking up a buttons individual click-count.正如弗拉基米尔在评论中暗示的那样,这是一个范围的问题。
变量计数仅在事件侦听器中声明,因此,稍后,当您按不同的按钮时,您的代码确实
count ++
对于不同的计数变量。要求解此声明
var count
而不是令count
- 将其全局。或在循环的
之前声明计数变量。
顺便说一句:确保停止更新
计数
,因为它不会更新count2
如果到4或更高as Vladimir implied in the comments its a scoping problem.
The variable count is only declared inside the event listener, so later on when you press different button your code does
count++
for a different count variable.To solve this declare
var count
instead oflet count
- that will make it global.Or declare the count variable before the
for
loop.BTW: make sure to stop updating
count
as it gets to 3, since it wouldn't updatecount2
if it gets to 4 or above