使用 for…in 语句遍历对象中的元素
- <!DOCTYPE html>
- <html>
- <body>
- <h1>JavaScript 循环</h1>
- <p>for/in 语句循环遍历对象的属性。</p>
- <p id="demo"></p>
- <script>
- var txt = "";
- var person = {fname:"Bill", lname:"Gates", age:62};
- var x;
- for (x in person) {
- txt += person[x] + " ";
- }
- document.getElementById("demo").innerHTML = txt;
- </script>
- </body>
- </html>