
批改狀態(tài):合格
老師批語:設(shè)置頁碼高亮是不是超級簡單, 就一個(gè)窗戶紙
代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="lib/jquery-3.5.1.js"></script>
<title>Ajax</title>
<style>
body {
display: grid;
gap: 15px;
}
button {
text-align: left;
height: 26px;
}
button:hover {
background-color: #ddd;
cursor: pointer;
}
</style>
</head>
<body>
<button type="button">1. load()請求數(shù)據(jù)</button>
<button type="button">2. $.get()請求數(shù)據(jù)</button>
<button type="button">3. $.post()請求數(shù)據(jù)</button>
<button type="button">4. $.getJSON()請求JSON數(shù)據(jù)</button>
<button type="button">5. $.ajax()請求數(shù)據(jù)</button>
<button type="button">6. $.ajax()-jsonp-跨域請求數(shù)據(jù)1</button>
<button type="button">7. $.ajax()-jsonp-跨域請求數(shù)據(jù)2</button>
</body>
</html>
<script>
var cl = console.log.bind(console);
// 1. load(): 加載html片斷
$("button:first-of-type").click(function () {
$(this).after("<div>").next().load("nav.html");
});
// 2. $.get():以get方式從服務(wù)器獲取資源/數(shù)據(jù)
$("button:nth-of-type(2)").click(function (ev) {
// $.get(url, data, callback)
$.get("users.php", { id: 2 }, function (data) {
// cl(data);
$(ev.target).after("<div>").next().html(data);
});
});
// 3. $.post():以post方式從服務(wù)器獲取資源/數(shù)據(jù)
$("button:nth-of-type(3)").click(function (ev) {
// $.post(url, data, callback)
$.post("users.php", { id: 4 }, function (data) {
// cl(data);
$(ev.target).after("<div>").next().html(data);
});
});
// 4. $.getJSON():接口返回的大多是JSON
$("button:nth-of-type(4)").click(function (ev) {
// $.getJSON(url, data, callback)
$.getJSON("users.php?id=2", function (data) {
// 返回就是js對象了, 不必轉(zhuǎn)換
// cl(JSON.parse(data));
cl(data);
var res = data.id + ": " + data.name + ", " + data.age + "歲";
$(ev.target).after("<div>").next().html(res);
});
});
// 5. $.ajax(): 終級方法, 實(shí)際上大家只需要掌握這一個(gè)方法
// $.ajax({
// // 請求類型
// type: "GET",
// // 請求的URL
// url: url,
// // 發(fā)送的數(shù)據(jù)
// data: data,
// // 期望服務(wù)器返回/響應(yīng)的數(shù)據(jù)類型
// dataType: "json",
// // 成功時(shí)的回調(diào)函數(shù)
// success: callback,
// });
$("button:nth-of-type(5)").click(function (ev) {
$.ajax({
type: "GET",
url: "users.php",
data: { id: 10 },
dataType: "html",
success: function (data) {
$(ev.target).after("<div>").next().html(data);
},
});
});
// "http://php.edu/0522/test2.php?jsonp=handle&id=3";
// 6. $.ajax()-jsonp-1:跨域請求
$("button:nth-of-type(6)").click(function (ev) {
$.ajax({
type: "GET",
url: "http://127.0.0.1/0522/test2.php?jsonp=?&id=2",
dataType: "jsonp",
success: function (data) {
cl(data);
var data = JSON.parse(data);
cl(data);
var data =
"<p>" +
data.title +
"</p><p>" +
data.user.name +
", 郵箱:" +
data.user.email +
"</p>";
$(ev.target).after("<div>").next().html(data);
},
});
});
// 7. $.ajax()-jsonp-2:跨域請求
$("button:last-of-type").click(function (ev) {
$.ajax({
type: "GET",
url: "http://127.0.0.1/0522/test2.php?jsonp=?&id=2",
dataType: "jsonp",
jsonpCallback: "handle",
});
});
function handle(data) {
cl(data);
var data = JSON.parse(data);
cl(data);
var data =
"<p>" +
data.title +
"</p><p>" +
data.user.name +
", 郵箱:" +
data.user.email +
"</p>";
$("button:last-of-type").after("<div>").next().html(data);
}
</script>
效果:
代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="lib/jquery-3.5.1.js"></script>
<title>Ajax無刷新分頁技術(shù)</title>
<style>
table {
border-collapse: collapse;
border: 1px solid;
text-align: center;
margin: auto;
width: 500px;
}
table caption {
font-size: 1.2rem;
margin-bottom: 10px;
}
th,
td {
border: 1px solid;
padding: 5px;
}
thead tr:first-of-type {
background-color: #ddd;
}
p {
text-align: center;
}
p a {
text-decoration: none;
border: 1px solid;
padding: 0 8px;
}
.active {
background-color: #ddd;
}
</style>
</head>
<body>
<table>
<caption>
消費(fèi)明細(xì)表表
</caption>
<thead>
<tr>
<th>id</th>
<th>金額</th>
<th>賬戶</th>
<th>成員</th>
<th>備注</th>
<th>用戶id</th>
</tr>
</thead>
<tbody></tbody>
</table>
<!-- 分頁條 -->
<p></p>
</body>
<script>
// 默認(rèn)是第1頁
var page = 1;
// 默認(rèn)顯示第一頁,用一個(gè)函數(shù)來實(shí)現(xiàn)顯示
getPageData(page);
// 分頁函數(shù)
function getPageData(page) {
// ajax無刷新分頁
$.ajax({
type: "post",
url: "page_data.php",
data: { page: page },
dataType: "json",
success: show,
});
}
// 數(shù)據(jù)顯示函數(shù)
function show(data) {
// 1. 顯示表格數(shù)據(jù)
console.log(data);
console.log(data.pages);
console.log(data.staffs);
var str = "";
data.staffs.forEach(function (staff) {
str += "<tr>";
str += "<td>" + staff.id + "</td>";
str += "<td>" + staff.jine + "</td>";
str += "<td>" + staff.zhanghu + "</td>";
str += "<td>" + staff.chengyuan + "</td>";
str += "<td>" + staff.beizhu + "</td>";
str += "<td>" + staff.yonghuid + "</td>";
str += "</tr>";
});
// $("tbody").append(str);
$("tbody").html(str);
// 2. 顯示分頁條
var str = "";
for (var i = 1; i <= data.pages; i++) {
// $("<a>").attr("href", "").attr("data-index", i).html(i).appendTo("p");
str += '<a href="" data-index=' + i + ">" + i + "</a>";
}
// 將頁碼添到分頁條, 并將當(dāng)前頁置為高亮
$("p")
.html(str)
.find("a")
.eq(page - 1)
.addClass("active");
// 分頁條的點(diǎn)擊事件
$("p a").click(function (ev) {
// 禁用<a>的跳轉(zhuǎn)默認(rèn)行為
ev.preventDefault();
page = $(this).attr("data-index");
$("tbody").html("");
getPageData(page);
});
}
</script>
</html>
效果:
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號