<!--原网页地址:https://www.douyu.com/g_yz-->
<!--原网页数据接口地址:https://www.douyu.com/gapi/rknc/directory/yzRec/1-->
<!--本地web服务器地址:http://127.0.0.1:5000/douyu?page=1-->
<!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>
<style>
ul {
width: 100%;
margin: 0 auto;
display: flex;
justify-content: space-evenly;
}
ul > li {
list-style-type: none;
width: 20px;
height: 20px;
line-height: 20px;
background-color: pink;
margin: 10px;
text-align: center;
}
</style>
</head>
<body>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
<div id="content">
<div>
<img src="" alt="" />
<p></p>
</div>
</div>
<script>
let rootEle = document.getElementById("content");
var liList = document.getElementsByTagName("li");
for (let i = 0; i < liList.length; i++) {
liList[i].onclick = function () {
rootEle.innerHTML = "";
get(i);
};
}
console.log(liList);
function get(num) {
var xhr = new XMLHttpRequest();
xhr.open("get", `http://127.0.0.1:5000/douyu?page=${num}`);
xhr.send();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
var temp = JSON.parse(xhr.responseText);
for (const index of temp.data.rl) {
rootEle.innerHTML += `
<div>
<img src="${index.rs1}" alt="" />
<p>${index.nn}</p>
</div>
`;
}
}
};
}
get(0);
</script>
</body>
</html>
Ajax实现网页翻页更新数据效果
最新推荐文章于 2024-07-04 16:37:43 发布