三个js循环的关键字示例(for与while)
内容摘要
循环的三种写法:
<!doctype html>
<title>js循环 by phpstudy</title>
<meta charset="utf-8"/>
<meta name="keywords" content="js循环 by phpstudy" />
<meta name="des
<!doctype html>
<title>js循环 by phpstudy</title>
<meta charset="utf-8"/>
<meta name="keywords" content="js循环 by phpstudy" />
<meta name="des
文章正文
循环的三种写法:
<!doctype html> <title>js循环 by phpstudy</title> <meta charset="utf-8"/> <meta name="keywords" content="js循环 by phpstudy" /> <meta name="description" content="js循环 by phpstudy" /> </head> <body> //while循环 <script type="text/javascript"> i = 1; while (i <= 6) { document.write("<h" + i+">phpstudy,这是标题"+i); document.write("</h"+i+">"); i++; } </script> //do_whilel循环 <script type="text/javascript"> i = 1; do { document.write("<h" + i+">phpstudy.net ,这是标题"+i); document.write("</h"+i+">"); i++; } while(i<=6); </script> //for循环 <script type="text/javascript"> for(i=1;i<=6;i++) { document.write("<h"+i+">phpstudy,这是标题"+i); document.write("</h"+i+">"); } </script> </body> </html>
不同类型的循环
JavaScript 支持不同类型的循环:
•for - 循环代码块一定的次数
•for/in - 循环遍历对象的属性
•while - 当指定的条件为 true 时循环指定的代码块
•do/while - 同样当指定的条件为 true 时循环指定的代码块
For 循环
for 循环是您在希望创建循环时常会用到的工具。
下面是 for 循环的语法:
for (语句 1; 语句 2; 语句 3)
{
被执行的代码块
}
语句 1 在循环(代码块)开始前执行
语句 2 定义运行循环(代码块)的条件
语句 3 在循环(代码块)已被执行之后执行
实例
http://www.phpstudy.net/w3school/js/js_loop_for.htm
代码注释