一、JavaScript循环语句
1.for循环、for/in
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <script> var array = [1,2,3,4,5,6]; for(var i=0; i<6; i++){ document.write(array[i]+" "); } </script> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <script> var array = [1,2,3,4,5,6]; var i; for(i in array){ document.write(array[i]+"<br/>"); } </script> </body> </html>
2.while循环、do...while
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <script> var i = 1; while(i<10){ document.write("i="+i+"<br/>") i++; } </script> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <script> var i = 10; do { document.write("i="+i+"<br/>") i++; }while(i<10) </script> </body> </html>
时间: 2024-12-29 23:43:16