dom结构
<div class="buttons">
<button>1</button><button>2</button><button>3</button><button>4</button>
</div>
js代码
let buttons = document.querySelectorAll("button");
let map = new Map();
buttons.forEach((item, i) => {
item.addEventListener("click", () => {
if (map.has(i)) map.set(i, map.get(i) + 1);
else map.set(i, 1);
console.log(map);
});
});
输出结果