今天所学心得、笔记

1、jQuery实现留言本案例(todolist)

示例截图

示例代码

  1. <body>
  2. <header>
  3. <div class="header">
  4. <label for="add">ToDoList</label>
  5. <input type="text" id="add" placeholder="添加计划项目......" autofocus>
  6. </div>
  7. </header>
  8. <main>
  9. <div class="todo">
  10. <div class="title">
  11. <span class="titleText">正在进行</span>
  12. <span class="titleTip">0</span>
  13. </div>
  14. <ul class="todoList">
  15. <!-- <li>-->
  16. <!-- <div class="left">-->
  17. <!-- <input type="checkbox" name="">-->
  18. <!-- <span>111111111111111</span>-->
  19. <!-- </div>-->
  20. <!-- <a href="#" class="right">-</a>-->
  21. <!-- </li>-->
  22. </ul>
  23. </div>
  24. <div class="done">
  25. <div class="title">
  26. <span class="titleText">已完成</span>
  27. <span class="titleTip">0</span>
  28. </div>
  29. <ul class="doneList">
  30. <!-- <li>-->
  31. <!-- <div class="left">-->
  32. <!-- <input type="checkbox" name="">-->
  33. <!-- <span>111111111111111</span>-->
  34. <!-- </div>-->
  35. <!-- <a href="#" class="right">-</a>-->
  36. <!-- </li>-->
  37. </ul>
  38. </div>
  39. </main>
  40. <footer>
  41. <span>Copyright © 2021 16lz.com中文网</span>
  42. </footer>
  43. <script src="jquery-3.5.1.js"></script>
  44. <script>
  45. // 启动时,读取缓存数据,并渲染到页面
  46. loadToPage();
  47. // 获取input框输入的数据
  48. $("input#add").on("keydown", function (ev) {
  49. let addText = $(this).val();
  50. if(addText !== '') {
  51. // console.log(ev.keyCode);
  52. if(ev.keyCode == 13) {
  53. // console.log(addText);
  54. //获取本地存储的数据
  55. let localData = getData();
  56. localData.push({title: addText, done: false});
  57. //存储入本地缓存
  58. saveData(localData);
  59. loadToPage();
  60. $(this).val('');
  61. }
  62. }
  63. });
  64. //渲染数据,至页面
  65. function loadToPage() {
  66. // 总数,计数器
  67. let todoCount = 0;
  68. let doneCount = 0;
  69. //渲染前,先清空页面数据
  70. $(".todoList, .doneList").empty();
  71. // 获取本地缓存数据
  72. let localData = getData();
  73. // console.log(localData);
  74. // // 循环数据,渲染数据,至页面(原生写法)
  75. // localData.forEach((item, index) => {
  76. // // console.log(item);
  77. // $(".todoList").prepend(`<li><div class='left'><input type='checkbox'><span>${item}</span></div><a href='#' class='right' id="${index}">-</a></li>`);
  78. // });
  79. // 循环数据,渲染数据,至页面(jQuery写法, 要注意:数据与index在括号中的位置,与原生相反)
  80. $.each(localData, (index, item) => {
  81. if(!item.done) {
  82. $(".todoList").prepend(`<li><div class='left'><input type='checkbox' data-id="${index}"><span>${item.title}</span></div><a href='#' class='right' id="${index}">-</a></li>`);
  83. todoCount++;
  84. }else {
  85. $(".doneList").prepend(`<li><div class='left'><input type='checkbox' data-id="${index}" checked><span>${item.title}</span></div><a href='#' class='right' id="${index}">-</a></li>`);
  86. doneCount++;
  87. }
  88. })
  89. $(".todo .titleTip").text(todoCount);
  90. $(".done .titleTip").text(doneCount);
  91. }
  92. //存储列表数据,入本地缓存
  93. function saveData(data) {
  94. // 将列表数据,由对象格式转换成,字符串格式,存储到本地
  95. localStorage.setItem('todoList', JSON.stringify(data));
  96. }
  97. //读取本地存储列表数据
  98. function getData() {
  99. let data = localStorage.getItem('todoList');
  100. if(data) {
  101. // 本地存储里面的数据,由字符串格式转换成,对象格式
  102. return JSON.parse(data);
  103. }else {
  104. return [];
  105. }
  106. }
  107. // 删除列表中的数据
  108. $(".todoList, .doneList").on("click", "a", function () {
  109. // 获取a标签id
  110. let id = $(this).val("id").get(0).id;
  111. // console.log(id);
  112. // 获取本地缓存数据
  113. let localData = getData();
  114. // 删除数据
  115. localData.splice(id, 1);
  116. //存储数据,并接着重新渲染数据
  117. saveData(localData);
  118. // 重新渲染页面
  119. loadToPage();
  120. });
  121. //进行中,已完成,分组设置(修改done的值),在loadToPage()函数中进行分组显示
  122. $(".todoList, .doneList").on('click', 'input', function () {
  123. // 获取本地缓存数据
  124. let localData = getData();
  125. // console.log(localData);
  126. // 获取选中li的id
  127. let id = $(this).data("id");
  128. // console.log(id);
  129. // 修改对应数据的,done值
  130. localData[id].done = $(this).prop("checked");
  131. // console.log(localData);
  132. //存储数据,并接着重新渲染数据
  133. saveData(localData);
  134. // 重新渲染页面
  135. loadToPage();
  136. });
  137. </script>
  138. </body>

2、$.ajax实现,$.get,$.post,$.ajax实现jsonp跨域请求

示例截图

示例代码

  1. <body>
  2. <div>
  3. <button class="get">1. $.get(): 请求数据</button>
  4. <button class="post">2. $.post(): 请求数据</button>
  5. <button class="jsonp">3. $.ajax():JSONP: 跨域请求数据</button>
  6. </div>
  7. <script src="jquery-3.5.1.js"></script>
  8. <script>
  9. // 1. $.get(请求url,查询参数,成功回调)
  10. $('.get').click(function (ev) {
  11. $.get('users.php?id=2', function (data) {
  12. $(ev.target).after("<div></div>").next().html(data);
  13. })
  14. })
  15. // 2. post()
  16. $(".post").click(function (ev) {
  17. $.post("users.php", { id: 3 }, function (data) {
  18. console.log(data);
  19. $(ev.target).after("<div></div>").next().html(data);
  20. });
  21. });
  22. // 3. $.ajax实现jsonp跨域请求
  23. $(".jsonp").click(function (ev) {
  24. $.ajax({
  25. type: "get",
  26. url: "http://www.mycors.com/test.php?id=2&jsonp=?",
  27. dataType: "jsonp",
  28. // 告诉跨域访问的服务器需要返回的函数名称
  29. // jsonpCallback: "show",
  30. success: function (data) {
  31. console.log(data);
  32. $("button:last-of-type").after("<div>").next().html(`${data.name} [${data.email}}]`);
  33. },
  34. });
  35. });
  36. </script>
  37. </body>

更多相关文章

  1. 如何通过 Saltstack pillar组件定义与被控主机相关的任何数据?
  2. SpringBoot 2.0 中 HikariCP 数据库连接池原理解析
  3. 一句话锁定MySQL数据占用元凶
  4. Android中SQLite数据库介绍和使用
  5. Content Provider 基础
  6. Android的App列表之拖拽ListView(上)
  7. 微软Webcast课程下载软件iReaper正式登陆Android平台
  8. Sqlite的使用和一个简单的书籍管理系统(上)
  9. android 组件化架构读书笔记(六)数据存储

随机推荐

  1. 创建链表的小例子
  2. 让 Linux 启动时加载自己的驱动模块 .ko
  3. PXE高效能网络批量装机
  4. Linux网络和进程管理命令
  5. 【分享】4412开发板-嵌入式Linux开发需要
  6. 在linux bash do循环中保持变量的值
  7. [Linux OS] Ubuntu 16.04 上实时显示上下
  8. 小型web服务器thttpd的学习总结(上)&小型we
  9. linux-阿里云仓库搭建-搭建本地仓库-yum
  10. 如何在Linux下优雅的查询日志