<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Keywords" content="关键字,关键词">
<meta name="Description" content="描述和简介">
<title>Title</title>
<style type="text/css">
*{margin:0;padding:0;}
body,ul,li,ol,dl,dd,p,h1,h2,h3,h4,h5,h6{ margin:0;}
a{text-decoration:none;}
img{border:none;}
ol,ul{list-style:none;}
#box{
border: 1px solid red;
}
</style>
</head>
<body>
<!--
json的写法
大括号中写键值对
var a = {属性:值,属性:值}
-->
<div id="box"></div>
<script>
var oBox = document.getElementById("box");
var a = {
name : "女神",
sex : "女",
marry : false,
age : 18,
sayHellow : function (){
alert("hello!");
console.log(this);
},
weight : "53kg",
qq : 16618,
height : 161
};
//alert(a.name);
oBox.innerHTML = "名字: "+a.name+",年龄: "+a.age+",性别: "+a.sex+",婚姻状况: "+(a.marry?"已婚":"未婚");
alert(typeof a.age);
// a.sayHellow();
/*
window.dachui();
function dachui(){
alert("你好,大锤");
console.log(this);
};
*/
// alert(a["name"]);
/* var x = "name";
alert(a[x]);*/
/*
* 遍历json里面的属性,在不清楚json里有多少个属性时,可用for in循环
*
* */
for(var dachui in a){
//console.log(dachui);
//console.log(typeof dachui);
//console.log("属性: "+key+"值: "+a[key]);
};
/*
* for in 遍历数组
*
* */
/* var arr = ["1",4,7,9,0];
for(var key in arr){//key 的类型是string
console.log(typeof key);
};*/
/*
* 新增属性
*
* */
a.phone = "156";//新增的属性会按照字母的顺序排序
//console.log(a);
/*
* 删除属性
*
* */
//a.marry = ""; //清空,不是删除
delete a.marry;
//console.log(a);
/*
* 添加下标属性(数字属性)
*
* */
/* a[2] = "嘿嘿嘿";
a[0] = "哈哈哈哈";*/
/*
* json没有length属性
*
* */
a.length = "不是长度";
alert(a.length);
console.log(a);
</script>
</body>
</html>
js 、json 、数组遍历(for in)
最新推荐文章于 2023-11-02 09:48:14 发布