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