为什么获取id为play得到的变量不能放在最外面,做全局变量呢?我试了这样,onload函数不能直接使用该变量,为什么
var data = ['Phone5', 'Ipad', '三星笔记本', '佳能相机', '惠普打印机', '谢谢参与', '50元充值卡', '1000元超市购物券'],
timer = null,
flag=0;
window.onload = function() {
var play = document.getElementById('play'),
stop = document.getElementById('stop');//如果放在外面获取不到
play.onclick = playFun;
stop.onclick= stopFun;
document.onkeyup=function(event){//这里keyup前面有on
event=event||window.event;
if(event.keyCode==13){
if(flag==0){//比较是==
playFun();
flag=1;
}else{
stopFun();
flag=0;
}
}
}
}
function playFun() {
var title = document.getElementById('title'),
play = document.getElementById('play');
clearInterval(timer);
timer = setInterval(function() {
var random = Math.floor(Math.random() * data.length);
title.innerHTML = data[random];
}, 100);
play.style.background="#ccc";
flag=1;
}
function stopFun(){
clearInterval(timer);
var play=document.getElementById('play');
play.style.background="#333";
flag=0;
}