博客列表 >jQuery 事件与Ajax

jQuery 事件与Ajax

赵大叔
赵大叔原创
2020年06月01日 07:22:24706浏览

事件-用户登录验证实践

  1. <script>
  2. // 禁用button默认提交
  3. $("form").submit(function (ev) {
  4. ev.preventDefault();
  5. });
  6. // 用户名文本框绑定失去焦点事件
  7. var user = $("input[type=text]");
  8. //blur(): 失去焦点事件
  9. user.blur(function () {
  10. // 提示
  11. var tips = "";
  12. // 用户名列表
  13. var users = ["oanh", "lam", "bao"];
  14. // 非空验证
  15. if ($(this).val().length === 0) {
  16. tips = "用户名不能为空";
  17. $(this).focus();
  18. // indexOf(): 在数组中查询是否存在某个值,存在则返回索引,否则返回-1
  19. } else if (users.indexOf($(this).val()) === -1) {
  20. tips = "用户名不存在" + "<button type='button'>请注册</button>";
  21. $(this).focus();
  22. } else {
  23. tips = '<i style="color: green">用户名正确</i>';
  24. $("input[type=password]").focus();
  25. }
  26. // 将提示信息添加到页面上
  27. // 防止提示信息重复添加
  28. if ($(this).next().get(0).tagName !== "SPAN")
  29. $("<span>").html(tips).css("color", "red").insertAfter($(this));
  30. user.keydown(function () {
  31. $(this).next("span").remove();
  32. });
  33. });
  34. </script>

效果图:

Ajax

1、load()请求数据
2、$.get()请求数据
3、$.post()请求数据
4、$.getJSON()请求JSON数据
5、$.ajax()请求数据
6、$.ajax()-jsonp-跨域请求数据1
7、$.ajax()-jsonp-跨域请求数据2

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>jQuery Ajax异步</title>
  6. <script src="lib/jQuery_v3.5.1.js"></script>
  7. <style>
  8. body {display: grid;gap: 15px;}
  9. button {text-align: left;width: 200px;height: 26px;}
  10. button:hover {background-color: palevioletred;cursor: pointer;}
  11. </style>
  12. </head>
  13. <body>
  14. <button type="button">1、load()请求数据</button>
  15. <button type="button">2、$.get()请求数据</button>
  16. <button type="button">3、$.post()请求数据</button>
  17. <button type="button">4、$.getJSON()请求JSON数据</button>
  18. <button type="button">5、$.ajax()请求数据</button>
  19. <button type="button">6、$.ajax()-jsonp-跨域请求数据1</button>
  20. <button type="button">7、$.ajax()-jsonp-跨域请求数据2</button>
  21. </body>
  22. <script>
  23. // 1、load()请求数据
  24. $("button:first-of-type").click(function () {
  25. $(this).after("<div>").next().load("nav.html");
  26. });
  27. // 2、$.get()请求数据
  28. $("button:nth-of-type(2)").click(function (ev) {
  29. // $.get(url, data, callback)
  30. $.get("user.php", { id: 2 }, function (data) {
  31. // console.log(data);
  32. // console.log($(this));
  33. $(ev.target).after("<div>").next().html(data);
  34. });
  35. });
  36. // 3、$.post()请求数据
  37. $("button:nth-of-type(3)").click(function (ev) {
  38. // $.post(url, data, callback)
  39. $.post("user.php", { id: 4 }, function (data) {
  40. // console.log(data);
  41. $(ev.target).after("<div>").next().html(data);
  42. });
  43. });
  44. // 4、$.getJSON()请求JSON数据
  45. $("button:nth-of-type(4)").click(function (ev) {
  46. // $.getJSON(url, data, callback)
  47. $.getJSON("user.php?id=2", function (data) {
  48. // 返回就是js对象了, 不必转换
  49. // cl(JSON.parse(data));
  50. // console.log(data);
  51. var res = data.id + ": " + data.name + ", " + data.age + "岁";
  52. $(ev.target).after("<div>").next().html(res);
  53. });
  54. });
  55. // 5、$.ajax()请求数据
  56. /* $.ajax语法:
  57. $.ajax({
  58. type: "GET", // 请求类型
  59. url: url, // 请求的URL
  60. data: data, // 发送的数据
  61. dataType: "json", // 期望服务器返回/响应的数据类型
  62. success: callback, // 成功时的回调函数
  63. });
  64. */
  65. $("button:nth-of-type(5)").click(function (ev) {
  66. $.ajax({
  67. type: "GET",
  68. url: "user.php",
  69. data: { id: 4 },
  70. dataType: "html",
  71. success: function (data) {
  72. $(ev.target).after("<div>").next().html(data);
  73. },
  74. });
  75. });
  76. // 6、$.ajax()-jsonp-跨域请求数据1
  77. $("button:nth-of-type(6)").click(function (ev) {
  78. $.ajax({
  79. type: "GET",
  80. url: "http://php.io/0518/test2.php?jsonp=?&id=2",
  81. dataType: "jsonp",
  82. success: function (data) {
  83. console.log(data);
  84. var data = JSON.parse(data);
  85. console.log(data);
  86. var data = "<p>" + data.title + "</p><p>" + data.user.name + ", 邮箱:" + data.user.email + "</p>";
  87. $(ev.target).after("<div>").next().html(data);
  88. },
  89. });
  90. });
  91. // 7、$.ajax()-jsonp-跨域请求数据2
  92. $("button:last-of-type").click(function (ev) {
  93. $.ajax({
  94. type: "GET",
  95. url: "http://php.io/0518/test2.php?jsonp=?&id=2",
  96. dataType: "jsonp",
  97. jsonpCallback: "handle",
  98. });
  99. });
  100. function handle (data) {
  101. console.log(data);
  102. var data = JSON.parse(data);
  103. console.log(data);
  104. var data = "<p>" + data.title + "</p><p>" + data.user.name + ", 邮箱:" + data.user.email + "</p>";
  105. $("button:last-of-type").after("<div>").next().html(data);
  106. }
  107. </script>
  108. </html>

效果图:

Ajax无刷新分页演示

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Ajax无刷新分页技术</title>
  6. <script src="lib/jQuery_v3.5.1.js"></script>
  7. <style>
  8. table {border-collapse: collapse; border: 1px solid;text-align: center; margin: auto;}
  9. caption {font-size: 1.2rem; margin-bottom: 10px;}
  10. thead > tr { background-color:wheat;}
  11. td,th {border: 1px solid; padding:5px}
  12. p {text-align: center;}
  13. p a {text-decoration: none;border: 1px solid;padding: 0 8px;margin: 5px;}
  14. .active {background-color: palevioletred;}
  15. </style>
  16. </head>
  17. <body>
  18. <table>
  19. <caption>
  20. 员工工资信息表
  21. </caption>
  22. <thead>
  23. <tr>
  24. <th>ID</th>
  25. <th>工号</th>
  26. <th>姓名</th>
  27. <th>入职日期</th>
  28. <th>部门</th>
  29. <th>岗位</th>
  30. <th>工作天</th>
  31. <th>应发</th>
  32. </tr>
  33. </thead>
  34. <tbody></tbody>
  35. </table>
  36. <!-- 分页条 -->
  37. <p></p>
  38. </body>
  39. <script>
  40. // 默认是第1页
  41. var page = 1;
  42. // 默认显示第一页,用一个函数来实现显示
  43. getPageData(page);
  44. // 分页函数
  45. function getPageData(page) {
  46. // ajax无刷新分页
  47. $.ajax({
  48. type: "post",
  49. url: "data_page.php",
  50. data: { page: page },
  51. dataType: "json",
  52. success: show,
  53. });
  54. }
  55. // 数据显示函数
  56. function show(data) {
  57. // 1. 显示表格数据
  58. // console.log(data);
  59. // console.log(data.pages);
  60. // console.log(data.salary);
  61. var str = "";
  62. data.salary.forEach(function (salary) {
  63. str += "<tr>";
  64. str += "<td>" + salary.id + "</td>";
  65. str += "<td>" + salary.msnv + "</td>";
  66. str += "<td>" + salary.name + "</td>";
  67. str += "<td>" + salary.hiredate + "</td>";
  68. str += "<td>" + salary.donvi + "</td>";
  69. str += "<td>" + salary.congviec + "</td>";
  70. str += "<td>" + salary.ngaycong + "</td>";
  71. str += "<td>" + salary.salary + "</td>";
  72. str += "</tr>";
  73. });
  74. $("tbody").html(str);
  75. // 2. 显示分页条
  76. var str = "";
  77. for (var i = 1; i <= data.pages; i++) {
  78. str += '<a href="" data-index=' + i + ">" + i + "</a>";
  79. }
  80. // 将页码添到分页条, 并将第一个置为高亮
  81. // page:当前显示的页码
  82. // eq:从0开始
  83. $("p").html(str).find("a").eq(page - 1).addClass("active");
  84. // 分页条的点击事件
  85. $("p a").click(function (ev) {
  86. // 禁用<a>的跳转默认行为
  87. ev.preventDefault();
  88. page = $(this).attr("data-index");
  89. $("tbody").html("");
  90. getPageData(page);
  91. });
  92. }
  93. </script>
  94. </html>

效果图:

总结

1、两个月学习了很多,学会了很多,同时也发现还不会很多。
2、完成了一二两阶段的作业,收获很多。
3、第三阶段继续加油,坚持就是胜利。

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议