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

<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);
    })
})
posted @ 2019-12-28 21:31  问问大将军  阅读(333)  评论(0编辑  收藏  举报