for循环:
var i = 0;
for ( ; i<3; ) {
alert(1);
i++;
}
循环顺序:
1) var i=0;
2) i<3; 关键!!!!
3) 括号里面的所有代码
4) i++
for(var i=0; i<3; i++){
……
}
做一个简单的循环题目:
把数组arr['今天', '明天', '后天' ];里面的只值,循环输出到无序列表中
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 5 <title>无标题文档</title> 6 <style> 7 li { height:30px; border-bottom:1px solid #333; } 8 </style> 9 <script> 10 window.onload = function (){ 11 var oUl = document.getElementById('list'); 12 var aLi = oUl.getElementsByTagName('li'); 13 var arr = [ '今天', '明天', '后天' ]; 14 var len = arr.length; 15 16 /* 17 aLi[0].onclick = function (){ alert(1); }; 18 aLi[1].onclick = function (){ alert(1); }; 19 aLi[2].onclick = function (){ alert(1); }; 20 */ 21 // aLi.onclick = function (){ alert(1); }; 22 23 for( var i=0; i<len; i++ ){ 24 25 aLi[i].innerHTML = arr[i]; 26 27 aLi[i].onclick = function (){ 28 // alert(i); i => 3 29 }; 30 } 31 }; 32 </script> 33 </head> 34 35 <body> 36 37 <ul id="list"> 38 <li></li> 39 <li></li> 40 <li></li> 41 </ul> 42 43 </body> 44 </html>