HTML事件處理程序(現在不建議使用了):事件直接加在HTML代碼中
缺點:HTML和js代碼高耦合,如果修改,就要修改兩個地方--HTML元素內和script函數。
缺點:HTML和js代碼高耦合,如果修改,就要修改兩個地方--HTML元素內和script函數。
2017-04-26
對于@qq_沃德天維森陌拉莫帥yb_0 同學說的為了方便而不在參數里設置type,那也只是對于這個點擊事件來說方便了,如果想要設置其它事件,那還要一個個進函數里修改。這里加上是為了函數的抽象化,增加函數的復用性,以后想要設置其它事件了,直接在參數里設置就好了,這樣更方便調用。
2017-04-24
window.onload=function(){
play.onclick=playFun;
stop.onclick=stopFun;
document.onkeyup=keyFun;
}
function keyFun(event){
var event=event||window.event;
if(event.keyCode==13){
timer?stopFun():playFun();
}
};
play.onclick=playFun;
stop.onclick=stopFun;
document.onkeyup=keyFun;
}
function keyFun(event){
var event=event||window.event;
if(event.keyCode==13){
timer?stopFun():playFun();
}
};
2017-04-24
function playFun(){
if(timer) return;
timer=setInterval(function(){
var r=Math.floor(Math.random() * data.length);
title.innerHTML=data[r];
},50);
play.style.background='#999';
}
function stopFun(){
if(!timer) return;
clearInterval(timer);timer=null;
play.style.background='#036';
}
if(timer) return;
timer=setInterval(function(){
var r=Math.floor(Math.random() * data.length);
title.innerHTML=data[r];
},50);
play.style.background='#999';
}
function stopFun(){
if(!timer) return;
clearInterval(timer);timer=null;
play.style.background='#036';
}
2017-04-24