-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
121 lines (97 loc) · 2.83 KB
/
index.html
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<!DOCTYPE HTML>
<html>
<head>
<title>index</title>
<meta charset="utf-8">
<meta name="Author" content="海牙">
<link rel="stylesheet" href="css/AjaxPaging.css">
</head>
<script>
window.onload = function(){
var url = 'https://route.showapi.com/181-1';
var oUl = document.querySelector('.flex_row')
var pageNum = 0;
var cache = {};
getData()
pageList()
function pageList(){
/*for(var i=0,len=aLi.length;i<len;i++){
aLi[i].index = i+1;
aLi[i].onclick = function(){
console.log(this.index)
}
}*/
oUl.addEventListener('click', function(e){
if(e.target.tagName.toLocaleLowerCase() === 'li'){
pageNum = e.target.innerText;
if(pageNum in cache){
console.log('数据已缓存')
render(cache[pageNum])
}else{
getData()
}
}
})
}
function getData(){
console.time('获取数据~~')
var sendData = {
showapi_appid: '30603',
showapi_sign: '98960666afeb4992ae91971d13494090',
page:pageNum,
num:8
}
var params = []
for(var item in sendData){
params.push(item +'=' + sendData[item])
}
var postData = params.join('&');
var xhr = new XMLHttpRequest();
xhr.open('GET',url+'?'+postData,true);
xhr.send(null);
xhr.onreadystatechange = function(){
if(xhr.readyState===4&&xhr.status===200){
var dataPost = JSON.parse(xhr.response).showapi_res_body.newslist;
cache[pageNum] = dataPost;
render(dataPost)
}
}
console.timeEnd('获取数据~~')
};
function render(dataList){
var html ='';
for(var i=0,len=dataList.length;i<len;i++){
str=
`
<a href="${dataList[i].url}" class="items flex_row">
<div class="img"><img src="${dataList[i].picUrl}" alt=""></div>
<div class="bd">
<p class="label">${dataList[i].title}</p>
</div>
<div class="ft">></div>
</a>
`
html = html + str;
}
var oContent = document.getElementsByClassName('content')[0];
oContent.innerHTML = html;
}
}
</script>
<body>
<div class="wrap flex_column">
<div class="content flex_column">
</div>
<div class="page">
<ul class="flex_row">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
</ul>
</div>
</div>
</body>
</html>