批改状态:合格
老师批语:一期比一期难, 好好努力吧少年
<!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>事情</title><style>body{display: flex;flex-flow: column nowrap;align-items: center;}h2{width: 300px;text-align: center;box-sizing: border-box;}form{width:200px;height: 300px;display: grid;gap: 10px;display: flex;flex-flow: column nowrap;justify-content: space-evenly;align-items: center;align-content: center;}form > button:hover{color :rgb(221, 0, 0);}</style></head><body><h2>User login:</h2><form action=""><input type="text" placeholder="UserName" autofocus /><input type="password" placeholder="Password" /><button>submit</button></form></body></html><script>var cl =console.log.bind(console);// 表单事件的绑定$("form").submit(function(ev){ev.preventDefault();});// 获取文本框用户名// var user = $("input").first();// 获取文本框用户名var user =$("input[type=text]");// blur()失去焦点事件user.blur(function(){// 提示信息var tips="";// 用户列表var users=["admin","big","xiaoming"];// 非空验证if($(this).val().length === 0){tips="用户名不能为空";// 焦点落在input上$(this).focus();}// indexOf():在数组中查询是否存在某个值,存在返回索引,否则返回1else if (users.indexOf($(this).val()) === -1){tips="用户名不存在"+"<button type='button'>请注册</button>";// 设置焦点$(this).focus();}else{tips = '<i style="color:green">用户名真确</i>';// 焦点设置在密码框$("input[type=password]").focus();}// 判断是否存在提示信息if($(this).next().get(0).tagName !== 'SPAN')// 将提示信息添加到页面上$("<span>").html(tips).css("color", "red").insertAfter($(this));// user.on("keydown" ,function(){// $(this).next('span').remove();// })// 当重新点击,删除提示信息user.keydown(function(){$(this).next('span').remove();});});</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</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()请求数据</button><button type="button">2. $.get()请求数据</button><button type="button">3. $.post()请求数据</button><button type="button">4. $.getJSON()请求JSON数据</button><button type="button">5. $.ajax()请求数据</button><button type="button">6. $.ajax()-jsonp-跨域请求数据1</button><button type="button">7. $.ajax()-jsonp-跨域请求数据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方式从服务器获取资源/数据$("button:nth-of-type(2)").click(function (ev) {// $.get(url, data, callback)$.get("users.php", { id: 1 }, function (data) {// cl(data);// cl($(this));// cl($(ev.target));$(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: 2 }, 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对象了, 不必转换// cl(JSON.parse(data));// cl(data);var res = data.id + ": " + data.name + ", " + data.age + "岁";$(ev.target).after("<div>").next().html(res);});});// 5. $.ajax(): 终级方法, 实际上大家只需要掌握这一个方法// $.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: 4 },dataType: "html",success: function (data) {$(ev.target).after("<div>").next().html(data);},});});// http://localhost/0415php/0522/test2.php// 6. $.ajax()-jsonp-1:跨域请求$("button:nth-of-type(6)").click(function (ev) {$.ajax({type: "GET",url: "http://localhost/0415php/0522/test2.php?jsonp=handle&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);},});});// 7. $.ajax()-jsonp-2:跨域请求$("button:last-of-type").click(function (ev) {$.ajax({type: "GET",url: "http://localhost/0415php/0522/test2.php?jsonp=?&id=1",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>
<?php$users = [['id' => 1, 'name' => '熊大', 'age' => 20],['id' => 2, 'name' => '熊二', 'age' => 18],['id' => 3, 'name' => '光头强', 'age' => 38],];$id = intval($_REQUEST['id']);if (in_array($id, array_column($users, 'id'))) {foreach ($users as $user) {if ($user['id'] === $id) {vprintf('%s : %s %s 岁', $user);// $_getJSON()返回返回json格式字符串// echo json_encode($user);}}} else {echo '<span style="color: red">没有找到</span>';}

<!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: rgb(206, 101, 101);border: 1px solid red;color: white;}</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></html><script>var cl =console.log.bind(console);// 默认显示第一页var page=1;getPageData(page);function getPageData(page){$.ajax({type:'post',url:'page_data.php',data:{page:page},dataType:'json',success:show,});}function show(data){// cl(data);// cl(data.pages);// cl(data.staffs);var str = "";data.staffs.forEach(function(staff){str +="<tr>";str +="<td>" + staff.id + "</td>";str += "<td>" + 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>";});// cl(str);// $('tbody').append(str);$('tbody').html(str);var str = "";for ( var i = 1 ; i <= data.pages; i++) {// $('<a>').attr('href',"").attr('data-index',i).html(i).append("p");str += '<a href="" data-index=' + i + ">" + i + "</a> ";}// $("p").html(str).find("a").first().addClass("active");// 将页码添到分页条, 并将当前页置为高亮$("p").html(str).find("a").eq(page - 1).addClass("active");// cl(page);$("p a").click(function (ev) {ev.preventDefault();page = $(this).attr("data-index");// cl(page)$("tbody").html("");getPageData(page);// cl(page);});}</script>
<?php// 链接数据库、$pdo = new PDO('mysql:host=localhost;dbname=phpedu', 'root', 'root');// 获取页数$page=$_POST['page'] ?? 1;// 每页显示的条数$num = 8 ;// 每页显示的偏移量$offset = ($page -1) * $num;// 总页数$sql = "SELECT CEIL(COUNT(`id`)/{$num}) AS `total` FROM `staffs`";$pages = $pdo->query($sql)->fetch()['total'];// 分页数据$sql = "SELECT * FROM `staffs` LIMIT {$num} OFFSET {$offset}";$staffs = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);// print_r($staffs);echo json_encode(['pages'=>$pages,'staffs'=>$staffs]);die;

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号