日期:2014-05-16  浏览次数:20400 次

关于for…in语句代码
var man = {
   hands: two,
   legs: three,
   heads: one
};

for (var i in man)
{
document.write(man[i]);
document.write("<br />");
document.write(i);
document.write("<br />");
};

为什么我这么写什么都没有

而把two,three,one换成数字2,3,1就可以

------解决方案--------------------
在你的写法中,two、three、one 是变量,需要先定义,否则会出错
如果不是变量,那么应用引号括起来
------解决方案--------------------
首先  你的json对象写错了 (如果你的one、two、three是其它的变量  就没问题)

var man = {
   hands: "two",
   legs: "three",
   heads: "one"
};

试试这样输出:

for(var a in man){
    alert( a + " : " + man[a] );
}

------解决方案--------------------

var man = {
   hands: two,
   legs: three,
   heads: one
};

字符串变量赋值有错
------解决方案--------------------
如果你的one、 two、three  是要直接输出的字符,那么在定义的时候需要加引号,
而你问1,2 ,3 为什么就没有问题,因为换成1,2,3时,默认成int类型了,
ocument.write(一个int数字) 是可以正常输出的。