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