1. for循环
<!DOCTYPE html> <html> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <body> <script charset = "utf-8"> nameList = ["kl", "td", "mgeno", "bobo"]; for (var i = 0; i < nameList.length; i++) { document.write(nameList[i] + "<br>"); } </script> </body> </html>
结果:
2. for/in循环直接遍历循环对象的属性。
<!DOCTYPE html> <html> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <body> <script charset = "utf-8"> var manInfo = {name: "tim", age: 20, grade: 98}; for (var i in manInfo) { var info = i + ": " + manInfo[i]; document.write(info + "<br>"); } </script> </body> </html>
结果: