Blogger Information
Blog 28
fans 0
comment 0
visits 25936
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Ajax事件、Ajax跨域请求,无刷新分页技术
,多思曩惜,
Original
742 people have browsed it

Ajax中事件函数

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <script src="lib/jquery-3.5.1.js"></script>
  7. <title>事情</title>
  8. <style>
  9. body{
  10. display: flex;
  11. flex-flow: column nowrap;
  12. align-items: center;
  13. }
  14. h2{
  15. width: 300px;
  16. text-align: center;
  17. box-sizing: border-box;
  18. }
  19. form{
  20. width:200px;
  21. height: 300px;
  22. display: grid;
  23. gap: 10px;
  24. display: flex;
  25. flex-flow: column nowrap;
  26. justify-content: space-evenly;
  27. align-items: center;
  28. align-content: center;
  29. }
  30. form > button:hover{
  31. color :rgb(221, 0, 0);
  32. }
  33. </style>
  34. </head>
  35. <body>
  36. <h2>User login:</h2>
  37. <form action="">
  38. <input type="text" placeholder="UserName" autofocus />
  39. <input type="password" placeholder="Password" />
  40. <button>submit</button>
  41. </form>
  42. </body>
  43. </html>
  44. <script>
  45. var cl =console.log.bind(console);
  46. // 表单事件的绑定
  47. $("form").submit(function(ev){
  48. ev.preventDefault();
  49. });
  50. // 获取文本框用户名
  51. // var user = $("input").first();
  52. // 获取文本框用户名
  53. var user =$("input[type=text]");
  54. // blur()失去焦点事件
  55. user.blur(function(){
  56. // 提示信息
  57. var tips="";
  58. // 用户列表
  59. var users=["admin","big","xiaoming"];
  60. // 非空验证
  61. if($(this).val().length === 0){
  62. tips="用户名不能为空";
  63. // 焦点落在input上
  64. $(this).focus();
  65. }
  66. // indexOf():在数组中查询是否存在某个值,存在返回索引,否则返回1
  67. else if (users.indexOf($(this).val()) === -1){
  68. tips="用户名不存在"+"<button type='button'>请注册</button>";
  69. // 设置焦点
  70. $(this).focus();
  71. }else{
  72. tips = '<i style="color:green">用户名真确</i>';
  73. // 焦点设置在密码框
  74. $("input[type=password]").focus();
  75. }
  76. // 判断是否存在提示信息
  77. if($(this).next().get(0).tagName !== 'SPAN')
  78. // 将提示信息添加到页面上
  79. $("<span>").html(tips).css("color", "red").insertAfter($(this));
  80. // user.on("keydown" ,function(){
  81. // $(this).next('span').remove();
  82. // })
  83. // 当重新点击,删除提示信息
  84. user.keydown(function(){
  85. $(this).next('span').remove();
  86. });
  87. });
  88. </script>

Ajax跨域请求

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <script src="lib/jquery-3.5.1.js"></script>
  7. <title>Ajax</title>
  8. <style>
  9. body {
  10. display: grid;
  11. gap: 15px;
  12. }
  13. button {
  14. text-align: left;
  15. height: 26px;
  16. }
  17. button:hover {
  18. background-color: #ddd;
  19. cursor: pointer;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <button type="button">1. load()请求数据</button>
  25. <button type="button">2. $.get()请求数据</button>
  26. <button type="button">3. $.post()请求数据</button>
  27. <button type="button">4. $.getJSON()请求JSON数据</button>
  28. <button type="button">5. $.ajax()请求数据</button>
  29. <button type="button">6. $.ajax()-jsonp-跨域请求数据1</button>
  30. <button type="button">7. $.ajax()-jsonp-跨域请求数据2</button>
  31. </body>
  32. </html>
  33. <script>
  34. var cl = console.log.bind(console);
  35. // 1. load(): 加载html片断
  36. $("button:first-of-type").click(function () {
  37. $(this).after("<div>").next().load("nav.html");
  38. });
  39. // 2. $.get():以get方式从服务器获取资源/数据
  40. $("button:nth-of-type(2)").click(function (ev) {
  41. // $.get(url, data, callback)
  42. $.get("users.php", { id: 1 }, function (data) {
  43. // cl(data);
  44. // cl($(this));
  45. // cl($(ev.target));
  46. $(ev.target).after("<div>").next().html(data);
  47. });
  48. });
  49. // 3. $.post():以post方式从服务器获取资源/数据
  50. $("button:nth-of-type(3)").click(function (ev) {
  51. // $.post(url, data, callback)
  52. $.post("users.php", { id: 2 }, function (data) {
  53. cl(data);
  54. $(ev.target).after("<div>").next().html(data);
  55. });
  56. });
  57. // 4. $.getJSON():接口返回的大多是JSON
  58. $("button:nth-of-type(4)").click(function (ev) {
  59. // $.getJSON(url, data, callback)
  60. $.getJSON("users.php", { id: 2 }, function (data) {
  61. // 返回就是js对象了, 不必转换
  62. // cl(JSON.parse(data));
  63. // cl(data);
  64. var res = data.id + ": " + data.name + ", " + data.age + "岁";
  65. $(ev.target).after("<div>").next().html(res);
  66. });
  67. });
  68. // 5. $.ajax(): 终级方法, 实际上大家只需要掌握这一个方法
  69. // $.ajax({
  70. // // 请求类型
  71. // type: "GET",
  72. // // 请求的URL
  73. // url: url,
  74. // // 发送的数据
  75. // data: data,
  76. // // 期望服务器返回/响应的数据类型
  77. // dataType: "json",
  78. // // 成功时的回调函数
  79. // success: callback,
  80. // });
  81. $("button:nth-of-type(5)").click(function (ev) {
  82. $.ajax({
  83. type: "GET",
  84. url: "users.php",
  85. data: { id: 4 },
  86. dataType: "html",
  87. success: function (data) {
  88. $(ev.target).after("<div>").next().html(data);
  89. },
  90. });
  91. });
  92. // http://localhost/0415php/0522/test2.php
  93. // 6. $.ajax()-jsonp-1:跨域请求
  94. $("button:nth-of-type(6)").click(function (ev) {
  95. $.ajax({
  96. type: "GET",
  97. url: "http://localhost/0415php/0522/test2.php?jsonp=handle&id=1",
  98. dataType: "jsonp",
  99. success: function (data) {
  100. // cl(data);
  101. var data = JSON.parse(data);
  102. // cl(data);
  103. var data = "<p>" + data.title + "</p><p>" + data.user.name +", 邮箱:" +data.user.email +"</p>";
  104. $(ev.target).after("<div>").next().html(data);
  105. },
  106. });
  107. });
  108. // 7. $.ajax()-jsonp-2:跨域请求
  109. $("button:last-of-type").click(function (ev) {
  110. $.ajax({
  111. type: "GET",
  112. url: "http://localhost/0415php/0522/test2.php?jsonp=?&id=1",
  113. dataType: "jsonp",
  114. jsonpCallback: "handle",
  115. });
  116. });
  117. function handle(data) {
  118. // cl(data);
  119. var data = JSON.parse(data);
  120. // cl(data);
  121. var data = "<p>" + data.title + "</p><p>" + data.user.name +", 邮箱:" + data.user.email +"</p>";
  122. $("button:last-of-type").after("<div>").next().html(data);
  123. }
  124. </script>
  1. <?php
  2. $users = [
  3. ['id' => 1, 'name' => '熊大', 'age' => 20],
  4. ['id' => 2, 'name' => '熊二', 'age' => 18],
  5. ['id' => 3, 'name' => '光头强', 'age' => 38],
  6. ];
  7. $id = intval($_REQUEST['id']);
  8. if (in_array($id, array_column($users, 'id'))) {
  9. foreach ($users as $user) {
  10. if ($user['id'] === $id) {
  11. vprintf('%s : %s %s 岁', $user);
  12. // $_getJSON()返回返回json格式字符串
  13. // echo json_encode($user);
  14. }
  15. }
  16. } else {
  17. echo '<span style="color: red">没有找到</span>';
  18. }

Ajax无刷新分页技术

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <script src="lib/jquery-3.5.1.js"></script>
  7. <title>Ajax无刷新分页技术</title>
  8. <style>
  9. table {
  10. border-collapse: collapse;
  11. border: 1px solid;
  12. text-align: center;
  13. margin: auto;
  14. width: 500px;
  15. }
  16. table caption {
  17. font-size: 1.2rem;
  18. margin-bottom: 10px;
  19. }
  20. th,
  21. td {
  22. border: 1px solid;
  23. padding: 5px;
  24. }
  25. thead tr:first-of-type {
  26. background-color: #ddd;
  27. }
  28. p {
  29. text-align: center;
  30. }
  31. p a {
  32. text-decoration: none;
  33. border: 1px solid;
  34. padding: 0 8px;
  35. }
  36. .active {
  37. background-color: rgb(206, 101, 101);
  38. border: 1px solid red;
  39. color: white;
  40. }
  41. </style>
  42. </head>
  43. <body>
  44. <table>
  45. <caption>
  46. 员工信息表
  47. </caption>
  48. <thead>
  49. <tr>
  50. <th>id</th>
  51. <th>姓名</th>
  52. <th>年龄</th>
  53. <th>性别</th>
  54. <th>职位</th>
  55. <th>手机号</th>
  56. </tr>
  57. </thead>
  58. <tbody></tbody>
  59. </table>
  60. <!-- 分页条 -->
  61. <p></p>
  62. </body>
  63. </html>
  64. <script>
  65. var cl =console.log.bind(console);
  66. // 默认显示第一页
  67. var page=1;
  68. getPageData(page);
  69. function getPageData(page){
  70. $.ajax({
  71. type:'post',
  72. url:'page_data.php',
  73. data:{page:page},
  74. dataType:'json',
  75. success:show,
  76. });
  77. }
  78. function show(data){
  79. // cl(data);
  80. // cl(data.pages);
  81. // cl(data.staffs);
  82. var str = "";
  83. data.staffs.forEach(function(staff){
  84. str +="<tr>";
  85. str +="<td>" + staff.id + "</td>";
  86. str += "<td>" + staff.name + "</td>";
  87. str += "<td>" + staff.age + "</td>";
  88. str += "<td>" + staff.sex + "</td>";
  89. str +="<td>" +staff.position + "</td>";
  90. str += "<td>" + staff.mobile + "</td>";
  91. str +="</tr>";
  92. });
  93. // cl(str);
  94. // $('tbody').append(str);
  95. $('tbody').html(str);
  96. var str = "";
  97. for ( var i = 1 ; i <= data.pages; i++) {
  98. // $('<a>').attr('href',"").attr('data-index',i).html(i).append("p");
  99. str += '<a href="" data-index=' + i + ">" + i + "</a> ";
  100. }
  101. // $("p").html(str).find("a").first().addClass("active");
  102. // 将页码添到分页条, 并将当前页置为高亮
  103. $("p").html(str).find("a").eq(page - 1).addClass("active");
  104. // cl(page);
  105. $("p a").click(function (ev) {
  106. ev.preventDefault();
  107. page = $(this).attr("data-index");
  108. // cl(page)
  109. $("tbody").html("");
  110. getPageData(page);
  111. // cl(page);
  112. });
  113. }
  114. </script>
  1. <?php
  2. // 链接数据库、
  3. $pdo = new PDO('mysql:host=localhost;dbname=phpedu', 'root', 'root');
  4. // 获取页数
  5. $page=$_POST['page'] ?? 1;
  6. // 每页显示的条数
  7. $num = 8 ;
  8. // 每页显示的偏移量
  9. $offset = ($page -1) * $num;
  10. // 总页数
  11. $sql = "SELECT CEIL(COUNT(`id`)/{$num}) AS `total` FROM `staffs`";
  12. $pages = $pdo->query($sql)->fetch()['total'];
  13. // 分页数据
  14. $sql = "SELECT * FROM `staffs` LIMIT {$num} OFFSET {$offset}";
  15. $staffs = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
  16. // print_r($staffs);
  17. echo json_encode(['pages'=>$pages,'staffs'=>$staffs]);
  18. die;

总结

  • 学习了连个月,学会了很多知识,了解了很多知识,但是自己独立应用起来却困难重重。
  • 对于不会的需要多多学,多多练习
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:一期比一期难, 好好努力吧少年
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!