可以用for in来遍历对象,具体内容如下:
<script type="text/javascript">
var objs = {
name: "wang ",
sex: "female ",
hobby: "drinking ",
eat: "food "
}
for (var obj in objs){
if(objs.hasOwnProperty(obj)){ //只输出obj的私有属性。
document.write(obj, ":", objs[obj] )
}
}
</script>
此时输出的结果为:name:wang sex:female hobby:drinking eat:food
用for来遍历数组:
var arr = ["你 ","好 ","世界 ","你好 ","世 ","界 "]
for(var i = 0; i < arr.length; i++){
document.write(i+ ":" + arr[i])
}
输出的结果为:0:你 1:好 2:世界 3:你好 4:世 5:界