遍历数组

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <h1>JavaScript 数组</h1>
  6.  
  7. <p>遍历数组的最佳方法是使用标准的 for 循环:</p>
  8.  
  9. <p id="demo"></p>
  10.  
  11. <script>
  12. var fruits, text, fLen, i;
  13. fruits = ["Banana", "Orange", "Apple", "Mango"];
  14. fLen = fruits.length;
  15.  
  16. text = "<ul>";
  17. for (i = 0; i < fLen; i++) {
  18. text += "<li>" + fruits[i] + "</li>";
  19. }
  20. text += "</ul>";
  21.  
  22. document.getElementById("demo").innerHTML = text;
  23. </script>
  24.  
  25. </body>
  26. </html>