js中的in_for循环

<!DOCTYPE html>
<html>
<body>
<p>点击下面的按钮,循环遍历对象 "person" 的属性。</p>
<button onclick="myFunction()">点击这里</button>
<p id="demo"></p>


<script>
function myFunction()
{
var x;
var txt="";
var person={fname:"Bill",lname:"Gates",age:56};


for (x in person)
{
alert("x:"+x);
txt=txt + person[x];
alert("txt:"+txt);
}


document.getElementById("demo").innerHTML=txt;
}
</script>
</body>
</html>

结果:BillGates56

猜你喜欢

转载自dwdwgo.iteye.com/blog/2307541