使用 for…in 声明来遍历数组中的元素

  1. <html>
  2. <body>
  3. <script type="text/javascript">
  4. var x
  5. var mycars = new Array()
  6. mycars[0] = "Saab"
  7. mycars[1] = "Volvo"
  8. mycars[2] = "BMW"
  9.  
  10. for (x in mycars)
  11. {
  12. document.write(mycars[x] + "<br />")
  13. }
  14. </script>
  15. </body>
  16. </html>