标签:
和其他语言一样,JavaScript也有循环结构和选择结构
选择结构:
<script type="text/javascript"> for(var i=0;i<10;i++){ document.write(i + " "); // 输出结果:0 1 2 3 4 5 6 7 8 9 } </script>
<script type="text/javascript"> var array = [1,2,3,4,5,6]; var i = 0; for(i in array){ document.write(array[i] + ‘ ‘); i++; } // 输出结果为 1 2 3 4 5 6 </script>
<script type="text/javascript"> var array = [1,2,3,4,5,6]; var i = 0; while(i<array.length){ document.write(array[i] + ‘ ‘); i++; } // 输出结果为 1 2 3 4 5 6 </script>
<script type="text/javascript"> var array = [1,2,3,4,5,6]; var i = 0; do{ document.write(array[i] + ‘ ‘); i++; } while(i<0); // 输出结果为 1 </script>
<script type="text/javascript"> var array = [1,2,3,4,5,6]; array.name = ‘张三‘; for(var i = 0;i<array.length;i++){ document.write(array[i]+ ‘ ‘); } // 1 2 3 4 5 6 document.write(‘<br/>‘); var i=0; for(i in array){ document.write(array[i]+ ‘ ‘); i++; } // 1 2 3 4 5 6 张三 </script>
标签:
原文地址:http://my.oschina.net/mpf/blog/527233