Files
ClassContent/历届学生/front-end-team-10/项目练习/学生项目/冯凯/练习/3.3ajax/next.html
2024-09-27 02:06:13 +08:00

71 lines
1.4 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>Document</title>
</head>
<body>
<ul></ul>
<button>下一页</button>
</body>
<script>
var page = 1;
function ajax() {
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) {
var data = JSON.parse(xhr.response)
readList(data)
} else {
alert("当前网络状态不假")
}
}
}
}
function readList(list) {
console.log(list.result.data);
list.result.data.forEach(v => {
$("ul").innerHTML += `
<li>
<div class="title">${v.title}</div>
<img src="${v.preview}" alt="">
</li>
`;
});
}
$("button").onclick = () => {
page += 1;
ajax()
}
ajax()
function $(className) {
return document.querySelector(className)
}
function _(className) {
return document.querySelectorAll(className)
}
</script>
</html>