Map应用实例(统计每个button的点击次数)

元气小坏坏 提交于 2019-12-28 21:36:27
<h1 class="heading">
    <button>fire</button>
    <button>Dancer</button>
    <button>Ice Cream</button>
    <button>Hamburger</button>
    <button>Fish</button>
</h1>
// Map 统计button点击每个按钮的次数

const clickCounts = new Map();
const buttons = document.querySelectorAll('button');
buttons.forEach(button => {
    clickCounts.set(button,0)
    button.addEventListener('click',function(){
        const val = clickCounts.get(this)
        
        clickCounts.set(this,val+1)
        console.log(clickCounts);
    })
})
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!