標籤:title aaa nts png 隔行變色 htm img java ack
閉包:子函數可以使用父函數的局部變數
<html> <head> <title>閉包 </title> <script> function aaa(){//父函數 var a=12; function bbb(){//子函數 閉包 可以引用父函數的局部變數 alert(a); } bbb(); } aaa(); </script> </head> <body> </body></html>
隔行變色
<html> <head> <title>隔行變色 </title> <script> window.onload=function(){ var aLi=document.getElementsByTagName(‘li‘); for(var i=0;i<aLi.length;i++){ //i 0 1 2 3 4 5 ... if(i%2==0){ aLi[i].style.background=‘grey‘; } else aLi[i].style.background=‘‘; } } </script> </head> <body> <ul> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> </ul> </body></html>
分秒轉換器
<html> <head> <title>分秒轉換器 </title> <script> var s=1137; alert(parseInt(s/60)+‘分‘+s%60+‘秒‘); </script> </head> <body> </body></html>
switch語句
<html> <head> <title>switch語句</title> <script> var name="張三"; var sex=‘男‘; switch(sex){ case ‘男‘: alert(name+‘先生,您好!‘); break; case ‘女‘: alert(name+‘女士,您好!‘); break; default: alert(name+‘您好!‘);} </script> </head> <body> </body></html>
三目運算子
條件?語句1:語句2
<script> var a=12;//判斷奇數偶數 a%2==0?alert("偶數"):alert("奇數"); </script>
JavaScript入門第三天