gpt4 book ai didi

javascript - 创建一个多按钮唯一点击计数器

转载 作者:行者123 更新时间:2023-12-04 08:25:23 29 4
gpt4 key购买 nike

我想在页面上有几个按钮,当点击时,向总计数器添加 +1。但我希望这种点击效果是独一无二的:每个按钮最多只添加 +1。
我想到了这段代码,但无法让它工作。问题似乎出在为 nbbutton[y] 分配新值时。你有什么建议吗?
非常感谢 !

var button = document.getElementsByClassName("clickme");
var idbutton = document.getElementById("idbutton");
var nbbutton = [0,1,2,3,4];
count = 0;
for (var y=0;y<button.length;y++)
if (nbbutton[y] != "clicked") {
button[y].onclick = function() {
count += 1;
counter.innerHTML = "Total unique click: " + count;
nbbutton[y] = "clicked";
}
}
body {
margin: 0;
padding: 0;
font-family: "Lato", sans-serif;
font-size: 20pt;
font-weight: normal;
background: lightblue; /* For browsers that do not support gradients */
background: -webkit-linear-gradient(
-90deg,
lightblue,
black
); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(
-90deg,
lightblue,
black
); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(
-90deg,
lightblue,
black
); /* For Firefox 3.6 to 15 */
background: linear-gradient(-90deg, lightblue, black); /* Standard syntax */
}

.main {
margin: 100px auto;
text-align: center;
}

button {
padding: 20px;
background: transparent;
text-shadow: 1px 1px 1px #202020;
font-family: "Lato", sans-serif;
font-size: 18pt;
border: 1px solid lightblue;
color: lightblue;
}
<html>

<head>
<meta charset="UTF-8">
<title></title>
<link href='https://fonts.googleapis.com/css?family=Lato' rel='stylesheet' type='text/css'>
</head>

<body>
<div class="main">

<h3>Click Counter</h3>
<button class="clickme" id="idbutton">Click me</button>
<button class="clickme" id="idbutton2">Click me too</button>
<button id="counter">Total unique click: 0</button>

<h5>Good practice: no JavaScript code in HTML</h5>

</div>
</body>

</html>

最佳答案

您可以在继续并增加计数器之前切换一个类并检查它是否存在,这只是向现有代码添加几行代码而不是重写它

var button = document.getElementsByClassName("clickme");
var idbutton = document.getElementById("idbutton");
var nbbutton = [0, 1, 2, 3, 4];
count = 0;
for (var y = 0; y < button.length; y++)
button[y].onclick = function() {
if (!this.classList.contains('alreadyClicked')) {
count += 1;
counter.innerHTML = "Total unique click: " + count;
nbbutton[y] = "clicked";
this.classList.add("alreadyClicked");
} else {
console.log("You've already clicked me!")
}
}
body {
margin: 0;
padding: 0;
font-family: "Lato", sans-serif;
font-size: 20pt;
font-weight: normal;
background: lightblue;
/* For browsers that do not support gradients */
background: -webkit-linear-gradient( -90deg, lightblue, black);
/* For Safari 5.1 to 6.0 */
background: -o-linear-gradient( -90deg, lightblue, black);
/* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient( -90deg, lightblue, black);
/* For Firefox 3.6 to 15 */
background: linear-gradient(-90deg, lightblue, black);
/* Standard syntax */
}

.main {
margin: 100px auto;
text-align: center;
}

button {
padding: 20px;
background: transparent;
text-shadow: 1px 1px 1px #202020;
font-family: "Lato", sans-serif;
font-size: 18pt;
border: 1px solid lightblue;
color: lightblue;
}
<div class="main">

<h3>Click Counter</h3>
<button class="clickme" id="idbutton">Click me</button>
<button class="clickme" id="idbutton2">Click me too</button>
<button id="counter">Total unique click: 0</button>

<h5>Good practice: no JavaScript code in HTML</h5>

</div>

关于javascript - 创建一个多按钮唯一点击计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65287372/

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