67 lines
1.8 KiB
HTML
67 lines
1.8 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>案例</title>
|
|
</head>
|
|
|
|
<body>
|
|
<ul></ul>
|
|
|
|
<button>下一页</button>
|
|
</body>
|
|
|
|
<script>
|
|
var page = 1;
|
|
|
|
//用于请求数据
|
|
function getData() {
|
|
var xhr = new XMLHttpRequest();
|
|
xhr.open("GET", `http://127.0.0.1:3000/api/json/mechanismList?id=82&page=${page}`)
|
|
xhr.send()
|
|
xhr.onreadystatechange = () => {
|
|
if (xhr.readyState == 4) {
|
|
if (xhr.status == 200) {
|
|
//data 字符串类型的对象或数组传
|
|
var data = JSON.parse(xhr.response)//从response中取数据
|
|
rendList(data)
|
|
}
|
|
} else {
|
|
alert("当前网络不佳,请稍后重试!")
|
|
}
|
|
}
|
|
}
|
|
getData()
|
|
function rendList(list) {
|
|
// console.log(list.result.data);
|
|
//数据存放在result 中
|
|
console.log(list.result.data);
|
|
//console.log(data.status);
|
|
//console.log(xhr.response);
|
|
list.result.data.forEach((v => {
|
|
$("ul").innerHTML += `
|
|
<li>
|
|
<a href="./info.html?id=${v.id}&total=${v.total}">
|
|
<div class="title">${v.title}</div>
|
|
<img src="${v.preview}" alt="">
|
|
</a>
|
|
</li>
|
|
`
|
|
}))
|
|
}
|
|
$("button").onclick = () => {
|
|
page += 1
|
|
getData()
|
|
}
|
|
function $(className) {
|
|
return document.querySelector(className);
|
|
}
|
|
function _(className) {
|
|
return document.querySelectorAll(className);
|
|
}
|
|
</script>
|
|
|
|
</html> |