0%

第十天

第十天

Json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Json</title>
</head>
<body>
<script>
// restful架构,json
// xml

var json = {//对象
id:1,
link:"http://www.baidu.com",
img:"img/1.jpg",
content:"这是content",
time:"2019-09-10"
}
//key键,value值,键值对
for(var k in json){
console.log(k,json[k]);
}
console.log(json.id);
document.write(JSON.stringify(json));//将JSON变为字符串

var str = JSON.stringify(json);//字符串
var o = JSON.parse(str);//对象
console.log(o.link);
</script>
</body>
</html>

json实际应用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>json</title>
<style>
ul,p{
margin: 0;
padding: 0;
}
li{
list-style: none;
}
img{
display: block;
border: none;
}
a{
text-decoration: none;
}
.jlist{
display: flex;
}
.jlist>li{
width: 33.3%;
padding: 5px;
box-sizing: border-box;
font-size: 14px;
line-height: 22px;
}
.jlist>li img{
width: 100%;
}
.name{
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
color: #999;
}
.time{
color: #ccc;
}
</style>
</head>
<body>
<ul class="jlist">

</ul>
<script>
// restful架构,json,前端与后台相分离

// json加入网页:
// 固定个数:事先写好html
// 不固定个数,html都由js插入
var json = [{
id:1,
link:"http://www.baidu.com",
img:"../大作业/images/2.png",
content:"这是content",
time:"2019-09-10"
},
{
id:2,
link:"http://www.baidu.com",
img:"../大作业/images/2.png",
content:"这是content",
time:"2019-09-10"
},
{
id:3,
link:"http://www.baidu.com",
img:"../大作业/images/2.png",
content:"这是content",
time:"2019-09-10"
}];
var c = {
data:json,
date:"dddd"
};

var jlist = document.querySelector(".jlist");
for(let i=0;i<json.length;i++){
let hang = `
<a href="${json[i].link}">
<img src="${json[i].img}" alt="">
<p class="name">${json[i].content}</p>
<p class="time">${json[i].time}</p>
</a>
`;
let li = document.createElement("li");
li.innerHTML = hang;
jlist.appendChild(li);

}
</script>
</body>
</html>

基础BOM

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>bom</title>
<style>
#box{
height: 4000px;
background: linear-gradient(#000,#ffc);
}
</style>
</head>
<body>
<div id="box" name="box"></div>
<button>到顶部</button>
<button>到顶部</button>
<button>到顶部</button>
<button>到顶部</button>
<button>到顶部</button>
<script>
var btn = document.querySelectorAll("button");
var box = document.querySelector("#box");
btn[0].onclick = function(){
location.hash = "box";//根据name回到顶部
location.hash = "";
}
btn[1].onclick = function(){
window.scrollTo(0,0);
}
btn[2].onclick = function(){
var st = document.body.scrollTop || document.documentElement.scrollTop;
var timer = setInterval(function() {
console.log(st);
st-=30;
window.scrollTo(0,st);
if(st<0)clearInterval(timer);
}, 1);
}
btn[3].onclick = function(){
location.reload();
}
btn[4].onclick = function(){
location.href = "test1.html";
}
</script>
</body>
</html>