批改状态:合格
老师批语:jquery并不是万能的, 请注意
AJAX = 异步 JavaScript 和 XML(Asynchronous JavaScript and XML),AJAX 是与服务器交换数据的艺术,它在不重载全部页面的情况下,实现了对部分网页的更新。
jQuery load() 方法是简单但强大的 AJAX 方法。load() 方法从服务器加载数据,并把返回的数据放入被选元素中。
// 1. load(): 加载html片断$("button:first-of-type").click(function () {$(this).after("<div>").next().load("nav.html");});
jQuery get() 和 post() 方法用于通过 HTTP GET 或 POST 请求从服务器请求数据。GET 基本上用于从服务器获得(取回)数据。POST 也可用于从服务器获取数据。不过,POST 方法不会缓存数据,并且常用于连同请求一起发送数据。
注释:GET 方法可能返回缓存数据。
// 2. $.get():以get方式从服务器获取资源/数据$("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方式从服务器获取资源/数据$("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);});});
$("button:nth-of-type(4)").click(function (ev) {// $.getJSON(url, data, callback)$.getJSON("users.php?id=2", function (data) {// 返回就是js对象了, 不必转换// cl(JSON.parse(data));cl(data);var res = data.id + ": " + data.name + ", " + data.age + "岁";$(ev.target).after("<div>").next().html(res);});});
// $.ajax({// // 请求类型// type: "GET",// // 请求的URL// url: url,// // 发送的数据// data: data,// // 期望服务器返回/响应的数据类型// dataType: "json",// // 成功时的回调函数// 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);},});});
$("button:nth-of-type(6)").click(function (ev) {$.ajax({type: "GET",url: "http://php.edu/0522/test2.php?jsonp=?&id=1",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);},});});
$("button:last-of-type").click(function (ev) {$.ajax({type: "GET",url: "http://php.edu/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);}
<?php// 1. 连接数据库$pdo = new PDO('mysql:host=localhost;dbname=php', 'root', 'root');// 2. 获取页码$page = $_POST['page'] ?? 1;// 3. 每页显示数量$num = 8;// 4. 每页显示偏移量$offset = ($page - 1) * $num;// 5. 总页数$sql = "SELECT CEIL(COUNT(`id`)/{$num}) AS `total` FROM `staffs`";// echo $sql;$pages = $pdo->query($sql)->fetch()['total'];// echo $pages;// 6. 分页数据$sql = "SELECT * FROM `staffs` LIMIT {$num} OFFSET {$offset}";$staffs = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);// print_r($staffs);// ajax分页数据一定是返回二部分// 1. 总页数, 提供给前端自动生成分页条// 2. 分页数据echo json_encode(['pages' => $pages, 'staffs' => $staffs]);die;
<!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>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>员工信息表</caption><thead><tr><th>id</th><th>姓名</th><th>年龄</th><th>性别</th><th>职位</th><th>手机号</th></tr></thead><tbody></tbody></table><!-- 分页条 --><p></p></body><script>// 默认是第1页var page = 1;// 默认显示第一页,用一个函数来实现显示getPageData(page);// 分页函数function getPageData(page) {// ajax无刷新分页$.ajax({type: "post",url: "page_data.php",data: { page: page },dataType: "json",success: show,});}// 数据显示函数function show(data) {// 1. 显示表格数据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><input type='text' value=" + staff.name + "></td>";str += "<td>" + staff.age + "</td>";str += "<td>" + staff.sex + "</td>";str += "<td>" + staff.position + "</td>";str += "<td>" + staff.mobile + "</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>";}// 将页码添到分页条, 并将第一个置为高亮$("p").html(str).find("a").first().addClass("active");// 分页条的点击事件$("p a").click(function (ev) {// 禁用<a>的跳转默认行为ev.preventDefault();var page = $(this).attr("data-index");$("tbody").html("");getPageData(page);});}</script></html>
编写常规的 AJAX 代码并不容易,因为不同的浏览器对 AJAX 的实现并不相同,必须编写额外的代码对浏览器进行测试。不过,jQuery 团队为我们解决了这个难题,我们只需要一行简单的代码,就可以实现 AJAX 功能。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号