js作用域實現騰訊首頁隔行換色效果
<html>
<head>
<meta charset="utf-8">
<title>騰訊首頁隔行換色</title>
<style>
h1{ height:30px; width:300px; font-size:25px; line-height:30px; text-align:center; border:2px solid pink; margin:40px auto;}
li{ height:30px; margin-bottom:5px; list-style:none;}
</style>
<script>
window.onload = function(){
var oUl = document.getElementById('list');
var aLi = oUl.getElementsByTagName('li');
var arr = ['yellow','red','pink'];
var str = ''; //定義一個變量值進行存儲
for(var i=0;i<aLi.length;i++){
aLi[i].index = i; //利用索引值進行實現
aLi[i].style.background = arr[i%arr.length];
aLi[i].onmouseover = function(){
str = this.style.background;
this.style.background = 'gray';
}
aLi[i].onmouseout = function(){
for(var i=0;i<aLi.length;i++){
aLi[i].style.background = arr[i%3];
}
}
aLi[i].onmouseout = function(){
this.style.background = arr[this.index%arr.length];
}
aLi[i].onmouseout = function(){
this.style.background = str;
}
}
}
</script>
</head>
<body>
<h1>js的%運算</h1>
<ul id="list">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</body>
</html>
alert(Math.floor(s/60)+'分'+s%60+'秒');
<html>
<head>
<meta charset="utf-8">
<title>js作用域的練習</title>
<style>
h1{ height:30px; width:300px; font-size:25px; line-height:30px; text-align:center; border:2px solid pink; margin:40px auto;}
</style>
<script>
window.onload = function(){
var aInp = document.getElementsByTagName('input');
for( var i=0;i<aInp.length;i++){
aInp[i].onclick = function(){
alert(i); //後面無var時值爲3,後面有var時值爲undefined
for( var i=0;i<aInp.length;i++){
aInp[i].style.background = 'yellow';
}
}
}
}
</script>
</head>
<body>
<h1>js的作用域的練習</h1>
<input type="button" value="1" />
<input type="button" value="2" />
<input type="button" value="3" />
</body>
</html>