给被点击的对象添加一个点击事件监听器,每次点击时计数器加1并更新显示出来。示例如下:
HTML:
点击次数:0
JavaScript: const clickBtn = document.getElementById('clickBtn'); const clickCount = document.getElementById('clickCount'); let count = 0;
clickBtn.addEventListener('click', () => { count++; clickCount.innerHTML = count; });