JavaScript中可以在点击事件上绑定函数,点击时执行函数,通过统计函数执行次数来统计点击次数。
JavaScript中统计点击次数:
Document
#Odiv{
margin:0 auto;
width: 200px;
height:200px;
background: yellow;
}
点击0次
var time=0;
function show(){
if(time%2==0){
Odiv.style.background="pink";
console.log(time);
time++;
}else{
Odiv.style.background="yellow";
console.log(time);
time++;
}
document.getElementById('Odiv').innerHTML = "点击"+time+"次";
}
实现效果如下:
JavaScript点击事件的写法:
第一种:btn.οnclick=function(){
alert('hello world');
}
第二种:btn.addEventListener('click',function(){alert('hello world')},false);
btn.addEventListener('click',function(){alert(this.id)},false);
第三种:function demo(){
alert('hello');
}
click