一、简易跑马灯
代码如下(点击按钮后会改变背景颜色):
html:
<div id="box">
<button>跑马灯</button>
<button>跑马灯</button>
<button>跑马灯</button>
<button>跑马灯</button>
<button>跑马灯</button>
<button>跑马灯</button>
<button>跑马灯</button>
<button>跑马灯</button>
<button>跑马灯</button>
<button>跑马灯</button>
</div>
js:
function horseRaceLamp() {
var btns = document.getElementById("box").children;
for(var i = 0; i < btns.length; i++) {
btns[i].onclick=function() {
setInterval(function() {
this.style.background = "#" +Math.round(16777215*Math.random()).toString(16);
}.bind(this),1000)
}
}
}
总结
纸上得来终觉浅,绝知此事要躬行