- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要有多个按钮,每个按钮在单击时都有自己的值,以便显示每天购买了多少特定产品。现在的问题是,尽管它们都有唯一的 ID,但它们加起来的总和相同。
我尝试了多种不同的方法来编写脚本,但它们都加到相同的总和上。
var clicks = 0;
function myFunction1(val) {
count = clicks += 1;
document.getElementById("demo1").innerHTML = clicks;
}
var clicks = 0;
function myFunction2() {
clicks += 1;
document.getElementById("demo2").innerHTML = clicks;
}
var clicks = 0;
function myFunction3() {
clicks += 1;
document.getElementById("demo3").innerHTML = clicks;
}
var clicks = 0;
function myFunction4() {
clicks += 1;
document.getElementById("demo4").innerHTML = clicks;
}
var clicks = 0;
function myFunction5() {
clicks += 1;
document.getElementById("demo5").innerHTML = clicks;
}
var clicks = 0;
function myFunction6() {
clicks += 1;
document.getElementById("demo6").innerHTML = clicks;
}
var clicks = 0;
function myFunction7() {
clicks += 1;
document.getElementById("demo7").innerHTML = clicks;
}
var clicks = 0;
function myFunction8() {
clicks += 1;
document.getElementById("demo8").innerHTML = clicks;
}
var clicks = 0;
function myFunction9() {
clicks += 1;
document.getElementById("demo9").innerHTML = clicks;
}
var clicks = 0;
function myFunction10() {
clicks += 1;
document.getElementById("demo10").innerHTML = clicks;
}
function myFunction11() {
clicks += 1;
document.getElementById("demo11").innerHTML = clicks;
}
var clicks = 0;
function myFunction12() {
clicks += 1;
document.getElementById("demo12").innerHTML = clicks;
}
var clicks = 0;
function myFunction13() {
clicks += 1;
document.getElementById("demo13").innerHTML = clicks;
}
var clicks = 0;
function myFunction14() {
clicks += 1;
document.getElementById("demo14").innerHTML = clicks;
}
var clicks = 0;
function myFunction15() {
clicks += 1;
document.getElementById("demo15").innerHTML = clicks;
}
var clicks = 0;
function myFunction16() {
clicks += 1;
document.getElementById("demo16").innerHTML = clicks;
}
var clicks = 0;
function myFunction17() {
clicks += 1;
document.getElementById("demo17").innerHTML = clicks;
}
body {
background: #97CE98;
}
.btn-group {
display: grid;
grid-template-columns: 4;
grid-row: 5 minmax(100px, auto);
grid-gap: 5px;
}
.one {
grid-column: 1/4;
grid-row: 1;
display: flex;
}
.two {
grid-column: 2/4;
grid-row: 1;
display: flex;
}
.three {
grid-column: 3/4;
grid-row: 1;
display: flex;
}
.four {
grid-column: 4/4;
grid-row: 1;
display: flex;
}
.five {
grid-column: 1/4;
grid-row: 2;
display: flex;
}
.six {
grid-column: 2/4;
grid-row: 2;
display: flex;
}
.seven {
grid-column: 3/4;
grid-row: 2;
display: flex;
}
.eight {
grid-column: 4/4;
grid-row: 2;
display: flex;
}
.nine {
grid-column: 1/4;
grid-row: 3;
display: flex;
}
.ten {
grid-column: 2/4;
grid-row: 3;
display: flex;
}
.eleven {
grid-column: 3/4;
grid-row: 3;
Display: flex;
}
.twelve {
grid-column: 4/4;
grid-row: 3;
display: flex;
}
.thirteen {
grid-column: 1/4;
grid-row: 4;
display: flex;
}
.fourteen {
grid-column: 2/4;
grid-row: 4;
display: flex;
}
.fifteen {
grid-column: 3/4;
grid-row: 4;
display: flex;
}
.sixteen {
grid-column: 4/4;
grid-row: 4;
display: flex;
}
.seventeen {
grid-column: 1/4;
grid-row: 5;
display: flex;
}
<p align="center">TPG Product Counter</p>
<div align="center" class="btn-group">
<div class="one">
<button onclick="myFunction1()" style="width:20%" id="demo1">Minced Hot 8oz </button>
</div>
<div class="two">
<button onclick="myFunction2()" style="width:20%" id="demo2">Minced Hot 16oz </button>
</div>
<div class="three">
<button onclick="myFunction3()" style="width:20%" id="demo3">Mindced Hot 24oz </button>
</div>
<div class="four">
<button onclick="myFunction4()" style="width:20%" id="demo4">Chunky Hot 8oz </button>
</div>
<div class="five">
<button onclick="myFunction5()" style="width:20%" id="demo5">Chunky Hot 16oz </button>
</div>
<div class="six">
<button onclick="myFunction6()" style="width:20%" id="demo6">Chunky Hot 24oz </button>
</div>
<div class="seven">
<button onclick="myFunction7()" style="width:20%" id="demo7">Hot Minced 8oz </button>
</div>
<div class="eight">
<button onclick="myFunction8()" style="width:20%" id="demo8">Hot Minced 8oz </button>
</div>
<div class="nine">
<button onclick="myFunction9()" style="width:20%" id="demo9">Hot Minced 8oz </button>
</div>
<div class="ten">
<button onclick="myFunction10()" style="width:20%" id="demo10">Hot Minced 8oz </button>
</div>
<div class="eleven">
<button onclick="myFunction11()" style="width:20%" id="demo11">Hot Minced 8oz </button>
</div>
<div class="twelve">
<button onclick="myFunction12()" style="width:20%" id="demo12">Hot Minced 8oz </button>
</div>
<div class="thirteen">
<button onclick="myFunction13()" style="width:20%" id="demo13">Hot Minced 8oz </button>
</div>
<div class="fourteen">
<button onclick="myFunction14()" style="width:20%" id="demo14">Hot Minced 8oz </button>
</div>
<div class="fifteen">
<button onclick="myFunction15()" style="width:20%" id="demo15">Hot Minced 8oz </button>
</div>
<div class="sixteen">
<button onclick="myFunction16()" style="width:20%" id="demo16">Hot Minced 8oz </button>
</div>
<div class="seventeen">
<button onclick="myFunction17()" style="width:20%" id="demo17">Hot Minced 8oz </button>
</div>
</div>
当您单击任何按钮时,它们的总和将全部相同,而不是具有唯一的总数。
最佳答案
当这可以用应用于所有 button
的 only 完成时,为什么会有如此多的功能,并且它将为每个按钮进行计数(按钮)单独:
button
添加相同的 class
(不是必需的,但它可以帮助我们根据 Angular 色对页面中的每个组件进行分类) 这将帮助我们在 JavaScript
中选择它们。JavaScript
中向这些 button
添加点击处理程序(从 HTML
中删除所有 onclick
属性) > 这将分别计算每个 button
的点击次数(每个 button
都有自己的计数器)。array
中,button
s 元素的数量(其长度)。下一个演示会帮助你更多,它还包含一些有用的评论:
/**
* btns: the buttons having the "btn-click-count" class (an array).
* btnsClicksCountArr: array to store the click counters for each button
**/
const btns = [...document.querySelectorAll('button.btn-click-count')],
btnsClicksCountArr = new Array(btns.length);
/** cycle through the buttons and apply a click handler for each one **/
/**
* el: the current button from the array of buttons (btns).
* i: its index in that array.
**/
btns.forEach((el, i) => {
/** initialize the btnsClicksCountArr at the index i with 0 **/
btnsClicksCountArr[i] = 0;
/** add click event handler for the current button in the array (btns) **/
el.addEventListener('click', () => el.textContent = ++btnsClicksCountArr[i]); /** increment the value stored in btnsClicksCountArr at the index i (the same index for that current button in the array btns) and write it in the button **/
});
body {
background: #97CE98;
}
.btn-group {
display: grid;
grid-template-columns: 4;
grid-row: 5 minmax(100px, auto);
grid-gap: 5px;
}
.one {
grid-column: 1/4;
grid-row: 1;
display: flex;
}
.two {
grid-column: 2/4;
grid-row: 1;
display: flex;
}
.three {
grid-column: 3/4;
grid-row: 1;
display: flex;
}
.four {
grid-column: 4/4;
grid-row: 1;
display: flex;
}
.five {
grid-column: 1/4;
grid-row: 2;
display: flex;
}
.six {
grid-column: 2/4;
grid-row: 2;
display: flex;
}
.seven {
grid-column: 3/4;
grid-row: 2;
display: flex;
}
.eight {
grid-column: 4/4;
grid-row: 2;
display: flex;
}
.nine {
grid-column: 1/4;
grid-row: 3;
display: flex;
}
.ten {
grid-column: 2/4;
grid-row: 3;
display: flex;
}
.eleven {
grid-column: 3/4;
grid-row: 3;
Display: flex;
}
.twelve {
grid-column: 4/4;
grid-row: 3;
display: flex;
}
.thirteen {
grid-column: 1/4;
grid-row: 4;
display: flex;
}
.fourteen {
grid-column: 2/4;
grid-row: 4;
display: flex;
}
.fifteen {
grid-column: 3/4;
grid-row: 4;
display: flex;
}
.sixteen {
grid-column: 4/4;
grid-row: 4;
display: flex;
}
.seventeen {
grid-column: 1/4;
grid-row: 5;
display: flex;
}
<!-- removed all the "onclick" attributes as this (attaching event listeners) will be placed in the JavaScript part -->
<!-- added a class of "btn-click-count" to all the buttons that will have counters -->
<p align="center">TPG Product Counter</p>
<div align="center" class="btn-group">
<div class="one">
<button class="btn-click-count" style="width:20%" id="demo1">Minced Hot 8oz </button>
</div>
<div class="two">
<button class="btn-click-count" style="width:20%" id="demo2">Minced Hot 16oz </button>
</div>
<div class="three">
<button class="btn-click-count" style="width:20%" id="demo3">Mindced Hot 24oz </button>
</div>
<div class="four">
<button class="btn-click-count" style="width:20%" id="demo4">Chunky Hot 8oz </button>
</div>
<div class="five">
<button class="btn-click-count" style="width:20%" id="demo5">Chunky Hot 16oz </button>
</div>
<div class="six">
<button class="btn-click-count" style="width:20%" id="demo6">Chunky Hot 24oz </button>
</div>
<div class="seven">
<button class="btn-click-count" style="width:20%" id="demo7">Hot Minced 8oz </button>
</div>
<div class="eight">
<button class="btn-click-count" style="width:20%" id="demo8">Hot Minced 8oz </button>
</div>
<div class="nine">
<button class="btn-click-count" style="width:20%" id="demo9">Hot Minced 8oz </button>
</div>
<div class="ten">
<button class="btn-click-count" style="width:20%" id="demo10">Hot Minced 8oz </button>
</div>
<div class="eleven">
<button class="btn-click-count" style="width:20%" id="demo11">Hot Minced 8oz </button>
</div>
<div class="twelve">
<button class="btn-click-count" style="width:20%" id="demo12">Hot Minced 8oz </button>
</div>
<div class="thirteen">
<button class="btn-click-count" style="width:20%" id="demo13">Hot Minced 8oz </button>
</div>
<div class="fourteen">
<button class="btn-click-count" style="width:20%" id="demo14">Hot Minced 8oz </button>
</div>
<div class="fifteen">
<button class="btn-click-count" style="width:20%" id="demo15">Hot Minced 8oz </button>
</div>
<div class="sixteen">
<button class="btn-click-count" style="width:20%" id="demo16">Hot Minced 8oz </button>
</div>
<div class="seventeen">
<button class="btn-click-count" style="width:20%" id="demo17">Hot Minced 8oz </button>
</div>
</div>
一些有用的链接:
Learn more about
addEventListener
function.Learn more about
forEach
function.
关于javascript - 多个点击计数器,每个点击计数器都有唯一值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57296814/
我知道我应该指定 argtypes对于我的 C/C++ 函数,因为我的某些调用会导致堆栈损坏。 myCfunc.argtypes = [ct.c_void_p, ct.POINTER(ct.c
我正在从 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators 学习 Itera
在我的数据库中,我有一个类别表。类别可以有父类别,使其成为递归关系 我还有一个产品表。每个产品都属于一个类别。 例如,我有一棵树,如下所示: Category Sub-Category 1
定义列表要求每个 会有标签? 例子: option1 每个 存在他的如果 空: value1 name2 value2 选项 2 每个 不存在他的如果空: value1 name2 value
我制作了一个脚本,它在开始时检查操作系统版本。 它读取/etc/os-release 文件,并检查操作系统是否为 CentOS 7。 但我不确定我是否可以确保每个 CentOS 7 都有那个文件。 其
我一直在使用 webapi 设置一个 mvc 项目。我可以正常工作,但我不知道为什么我的 GET 调用的 URL 中有附加部分。 示例: /api/User/Zholen?_=137349028082
我创建了具有 ACTIVE BOOL 的基类 class BaseTest{ public: bool active = false; BaseTest(){ // make
.parent { background-color: yellow; display: flex; justify-content: space-evenly; } .parent >
完整的错误是: AttributeError: Neither 'ColumnClause' object nor 'Comparator' object has an attribute 'desc
我的 GUI 中有一些功能可以随着时间的推移更新给定的文本组件。理想情况下,我希望它接受任何具有 getText/setText 方法的内容。使用 JTextField 和 JTextPane 可以轻
我是一名优秀的程序员,十分优秀!