使用 for…in 语句遍历对象中的元素

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <h1>JavaScript 循环</h1>
  6.  
  7. <p>for/in 语句循环遍历对象的属性。</p>
  8.  
  9. <p id="demo"></p>
  10.  
  11. <script>
  12. var txt = "";
  13. var person = {fname:"Bill", lname:"Gates", age:62};
  14. var x;
  15. for (x in person) {
  16. txt += person[x] + " ";
  17. }
  18. document.getElementById("demo").innerHTML = txt;
  19. </script>
  20.  
  21. </body>
  22. </html>