将eventlisteners添加到附加到数组项的多个按钮

7bsow1i6  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(274)

我试图创建一个简单的计数计数器,为“名称”数组中的每个人创建相同的按钮集。
javascript

var names = ["Andrew", "Ryan", "Brandon", "Jason", "Justin", "Sarah", "William", "Jonathan", "Stephanie", "Brian", "Nicole", "Nicholas", "Anthony", "Heather", "Eric", "Elizabeth", "Adam", "Megan", "Melissa", "Kevin", "Steven", "Thomas", "Timothy", "Christina", "Kyle", "Rachel", "Laura", "Lauren", "Amber", "Brittany", "Danielle", "Richard", "Kimberly", "Jeffrey", "Amy", "Crystal"];

function Buttons() {
    var List = document.getElementById('container');
    List.innerHTML=''
    names.forEach((name) => {
        List.innerHTML += name + '<div> <div class="counter">0</div> <button class="add">Add</button><button class="minus">Minus</button><br><button class="reset">Reset</button></div>'
    });
};

Buttons();

let counter = document.querySelectorAll(".counter");
let add = document.querySelectorAll(".add");
let minus = document.querySelectorAll(".minus");
let reset = document.querySelectorAll(".reset");

add.forEach(addButton => {
    addButton.addEventListener('click', addCounter);
});

minus.forEach(minusButton => {
    minusButton.addEventListener('click', minusCounter);
});

reset.forEach(resetButton => {
    resetButton.addEventListener('click', resetCounter);
});

function addCounter() {
    counterNum = counter.innerHTML
    counter.innerHTML = parseInt(counterNum) + 1
}

function minusCounter() {
    counterNum = counter.innerHTML
    if (counterNum == 0){
        return false
    }
    counter.innerHTML = parseInt(counterNum) - 1
}

function resetCounter() {
    counter.innerHTML = 0;
};

html

<body>
    <h1>Tally Counter</h1>
    <div id="container"></div>

    <script src="/script.js"></script>
</body>
</html>

当我点击按钮时,什么也没发生。当我将计数器变量上的queryselectorall更改为just queryselector时,它仅对第一人称有效。我做错了什么?有没有更好的方法?

gcmastyq

gcmastyq1#

您正在设置一个名为“counter”的变量,并为其分配div元素(所有class='counter'元素)的html集合。因此,您的单击侦听器函数不知道要使用哪个计数器。
访问与其他元素(如按钮)关联的元素(如计数器div)的最佳方法是使用相对路径,例如:单击按钮时,找到与其分组的计数器并修改它。你可以用它来做 closest() (遍历元素的父元素直到找到匹配项)与 querySelector() 这里是一个轻微的重写,希望向您展示如何最小化代码的易读性,以及如何访问所需的计数器。

const names = ["Andrew", "Ryan", "Brandon", "Jason", "Justin", "Sarah", "William", "Jonathan", "Stephanie", "Brian", "Nicole", "Nicholas", "Anthony", "Heather", "Eric", "Elizabeth", "Adam", "Megan", "Melissa", "Kevin", "Steven", "Thomas", "Timothy", "Christina", "Kyle", "Rachel", "Laura", "Lauren", "Amber", "Brittany", "Danielle", "Richard", "Kimberly", "Jeffrey", "Amy", "Crystal"];

function getCodeBlock(name) {
  return `<div class='block'>${name}<div> <div class="counter">0</div> <button class="add">Add</button><button class="minus">Minus</button><br><button class="reset">Reset</button></div></div>`
}
document.getElementById('container').innerHTML = names.map(getCodeBlock).join('')

document.querySelectorAll('button').forEach(b => {
  b.addEventListener('click', e => {
    let counter = e.target.closest('div').querySelector('.counter');
    if (e.target.classList.contains('add')) counter.innerHTML = +counter.innerHTML + 1;
    else if (e.target.classList.contains('minus')) counter.innerHTML = +counter.innerHTML - 1;
    else if (e.target.classList.contains('reset')) counter.innerHTML = '0'

  })
})
.block {
  padding: 5px;
  margin-bottom: 5px;
  border-bottom: 1px dashed #ccc;
}

.counter {
  font-size: 24px;
  margin: 2px 0;
}
<h1>Tally Counter</h1>
<div id="container"></div>

相关问题